Repository navigation
Expand file tree
/
Copy pathtest_cpp_attributes.py
More file actions
1244 lines (1015 loc) · 33.9 KB
/
Copy pathtest_cpp_attributes.py
File metadata and controls
1244 lines (1015 loc) · 33.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from pathlib import Path
from typing import cast
from unittest.mock import MagicMock
import pytest
from codebase_rag.tests.conftest import get_relationships, run_updater
@pytest.fixture
def cpp_attributes_project(temp_repo: Path) -> Path:
"""Create a comprehensive C++ project with attribute patterns."""
project_path = temp_repo / "cpp_attributes_test"
project_path.mkdir()
(project_path / "src").mkdir()
(project_path / "include").mkdir()
(project_path / "include" / "attributes.h").write_text(
encoding="utf-8", data="#pragma once\nnamespace attr_test {}"
)
return project_path
def test_standard_attributes(
cpp_attributes_project: Path,
mock_ingestor: MagicMock,
) -> None:
"""Test standard C++ attributes parsing and relationship tracking."""
test_file = cpp_attributes_project / "standard_attributes.cpp"
test_file.write_text(
encoding="utf-8",
data="""
#include <iostream>
#include <string>
#include <memory>
#include <vector>
#include <optional>
// [[nodiscard]] attribute testing
[[nodiscard]] int calculateImportantValue() {
return 42;
}
[[nodiscard("Don't ignore the result!")]]
std::string createImportantString() {
return "Critical data";
}
[[nodiscard]] constexpr bool isValid(int value) noexcept {
return value > 0;
}
// [[maybe_unused]] attribute testing
void processData([[maybe_unused]] int debugMode,
[[maybe_unused]] const std::string& logLevel) {
// Implementation might not use all parameters in release builds
#ifdef DEBUG
std::cout << "Debug mode: " << debugMode << ", Level: " << logLevel << std::endl;
#endif
}
[[maybe_unused]] static int globalCounter = 0;
[[maybe_unused]] static void helperFunction() {
// Utility function that might not be used in all builds
globalCounter++;
}
// [[deprecated]] attribute testing
[[deprecated]]
void oldFunction() {
std::cout << "This function is deprecated" << std::endl;
}
[[deprecated("Use newImprovedFunction() instead")]]
int legacyCalculation(int x, int y) {
return x + y;
}
[[deprecated("This class will be removed in v2.0")]]
class LegacyClass {
public:
[[deprecated("Use getNewValue() instead")]]
int getOldValue() const { return value_; }
int getNewValue() const { return value_; }
private:
int value_ = 0;
};
// [[fallthrough]] attribute testing
std::string processSwitch(int value) {
std::string result;
switch (value) {
case 1:
result += "one";
[[fallthrough]];
case 2:
result += "two";
[[fallthrough]];
case 3:
result += "three";
break;
case 4:
result += "four";
[[fallthrough]];
default:
result += "default";
break;
}
return result;
}
// Complex switch with multiple fallthrough patterns
int complexSwitch(char c) {
switch (c) {
case 'a':
case 'A':
std::cout << "Letter A";
[[fallthrough]];
case 'e':
case 'E':
std::cout << " (vowel)";
return 1;
case 'b':
std::cout << "Letter B";
[[fallthrough]];
case 'c':
std::cout << " (consonant)";
return 2;
default:
return 0;
}
}
// [[likely]] and [[unlikely]] attributes (C++20)
int processConditions(int value) {
if (value > 0) [[likely]] {
// Most common case
return value * 2;
} else if (value < -100) [[unlikely]] {
// Rare error case
throw std::runtime_error("Invalid value");
} else {
// Uncommon but not rare
return 0;
}
}
// Complex branching with likelihood hints
std::optional<int> parseInput(const std::string& input) {
if (input.empty()) [[unlikely]] {
return std::nullopt;
}
if (input.length() > 1000) [[unlikely]] {
// Very long input is rare
return std::nullopt;
}
// Normal processing path
if (std::isdigit(input[0])) [[likely]] {
try {
return std::stoi(input);
} catch (...) [[unlikely]] {
return std::nullopt;
}
}
return std::nullopt;
}
// Combining multiple attributes
[[nodiscard]] [[deprecated("Use modernCalculation() instead")]]
int legacyImportantCalculation(int x) {
return x * x;
}
[[nodiscard]]
constexpr int modernCalculation(int x) noexcept {
return x * x;
}
// Attributes on class members
class AttributeDemo {
public:
[[nodiscard]] bool isReady() const noexcept { return ready_; }
[[deprecated("Use setStatus() instead")]]
void setReady(bool ready) { ready_ = ready; }
void setStatus([[maybe_unused]] bool status) {
ready_ = status;
}
private:
[[maybe_unused]] static constexpr int VERSION = 1;
bool ready_ = false;
};
// Namespace-level attributes
namespace [[deprecated("Use modern_api namespace instead")]] legacy_api {
void oldStyleFunction() {
std::cout << "Legacy API function" << std::endl;
}
}
namespace modern_api {
[[nodiscard]] bool newStyleFunction() {
std::cout << "Modern API function" << std::endl;
return true;
}
}
// Function demonstrating attribute usage patterns
void demonstrateStandardAttributes() {
// Test nodiscard attributes
int result = calculateImportantValue(); // OK
calculateImportantValue(); // Warning: discarding nodiscard return value
std::string data = createImportantString(); // OK
createImportantString(); // Warning: discarding nodiscard return value
// Test maybe_unused
processData(1, "DEBUG"); // Parameters marked maybe_unused
// Test deprecated functions
oldFunction(); // Warning: deprecated
int sum = legacyCalculation(5, 3); // Warning: deprecated with message
LegacyClass legacy; // Warning: deprecated class
int oldVal = legacy.getOldValue(); // Warning: deprecated method
int newVal = legacy.getNewValue(); // OK
// Test fallthrough
std::string switchResult = processSwitch(2);
int charResult = complexSwitch('a');
// Test likely/unlikely
int condResult = processConditions(10); // Likely path
try {
int errorResult = processConditions(-200); // Unlikely path
} catch (const std::runtime_error& e) {
std::cerr << "Caught exception: " << e.what() << std::endl;
}
// Test optional parsing with likelihood
auto parsed1 = parseInput("123"); // Likely success
auto parsed2 = parseInput(""); // Unlikely empty input
auto parsed3 = parseInput("not_number"); // Normal failure case
// Test combined attributes
int legacyResult = legacyImportantCalculation(5); // Deprecated + nodiscard
int modernResult = modernCalculation(5); // Just nodiscard
// Test class attributes
AttributeDemo demo;
bool ready = demo.isReady(); // nodiscard
demo.setReady(true); // deprecated
demo.setStatus(true); // modern method
// Test namespace attributes
legacy_api::oldStyleFunction(); // Deprecated namespace
bool success = modern_api::newStyleFunction(); // Modern namespace
}
// Template with attributes
template<typename T>
[[nodiscard]] constexpr T multiply([[maybe_unused]] T a, T b) noexcept {
// In some specializations, parameter 'a' might not be used
return b * b; // Simplified implementation
}
// Specialization with different attribute usage
template<>
[[nodiscard]] constexpr int multiply<int>(int a, int b) noexcept {
return a * b; // Both parameters used in this specialization
}
void testTemplateAttributes() {
auto intResult = multiply<int>(3, 4); // Uses specialization
auto doubleResult = multiply<double>(2.5, 3.0); // Uses general template
// Test discarding nodiscard return
multiply<int>(5, 6); // Warning: discarding nodiscard return
}
""",
)
run_updater(cpp_attributes_project, mock_ingestor)
defines_relationships = get_relationships(mock_ingestor, "DEFINES")
call_relationships = get_relationships(mock_ingestor, "CALLS")
attributed_functions = [
call
for call in defines_relationships
if "standard_attributes" in call.args[0][2]
and any(
func in call.args[2][2]
for func in [
"calculateImportantValue",
"createImportantString",
"processData",
"oldFunction",
"legacyCalculation",
"processSwitch",
"processConditions",
]
)
]
assert len(attributed_functions) >= 7, (
f"Expected at least 7 attributed functions, found {len(attributed_functions)}"
)
attribute_function_calls = [
call
for call in call_relationships
if "standard_attributes" in call.args[0][2]
and any(
func in call.args[2][2]
for func in [
"calculateImportantValue",
"createImportantString",
"oldFunction",
]
)
]
assert len(attribute_function_calls) >= 3, (
f"Expected at least 3 attributed function calls, found {len(attribute_function_calls)}"
)
def test_compiler_specific_attributes(
cpp_attributes_project: Path,
mock_ingestor: MagicMock,
) -> None:
"""Test compiler-specific attributes and non-standard extensions."""
test_file = cpp_attributes_project / "compiler_attributes.cpp"
test_file.write_text(
encoding="utf-8",
data="""
#include <iostream>
#include <vector>
#include <string>
// GCC/Clang specific attributes using __attribute__ syntax
__attribute__((always_inline))
inline int fastFunction(int x) {
return x * 2;
}
__attribute__((noinline))
void slowFunction() {
std::cout << "This function should not be inlined" << std::endl;
}
__attribute__((pure))
int pureCalculation(int a, int b) {
// Pure function - no side effects, result depends only on arguments
return a + b * 2;
}
__attribute__((const))
constexpr int constantCalculation(int x) {
// Const function - even stricter than pure, no memory access
return x * x;
}
// Function alignment
__attribute__((aligned(32)))
void alignedFunction() {
std::cout << "Function aligned to 32-byte boundary" << std::endl;
}
// Hot/Cold attributes for optimization
__attribute__((hot))
void frequentlyCalledFunction() {
// This function is called frequently and should be optimized for speed
for (int i = 0; i < 1000; ++i) {
// Tight loop
}
}
__attribute__((cold))
void rarelyCalledFunction() {
// This function is rarely called, optimize for size instead of speed
std::cerr << "Error: This should rarely happen" << std::endl;
}
// Visibility attributes for shared libraries
__attribute__((visibility("default")))
void publicApiFunction() {
std::cout << "This function is part of the public API" << std::endl;
}
__attribute__((visibility("hidden")))
void internalFunction() {
std::cout << "This function is internal to the library" << std::endl;
}
// Constructor/Destructor attributes
__attribute__((constructor))
void moduleInitializer() {
std::cout << "Module constructor called" << std::endl;
}
__attribute__((destructor))
void moduleCleanup() {
std::cout << "Module destructor called" << std::endl;
}
// Variable attributes
__attribute__((aligned(64)))
static int alignedVariable = 42;
__attribute__((section(".special_data")))
static int specialSectionVariable = 100;
__attribute__((weak))
int weakSymbol = 200;
// Weak function that can be overridden
__attribute__((weak))
void weakFunction() {
std::cout << "Default weak implementation" << std::endl;
}
// Warning attributes
__attribute__((deprecated("Use newSecureFunction() instead")))
void insecureFunction(char* buffer) {
// Unsafe string operations
strcpy(buffer, "unsafe");
}
void newSecureFunction(std::string& buffer) {
buffer = "safe";
}
// Format checking attributes
__attribute__((format(printf, 1, 2)))
void debugPrintf(const char* format, ...) {
// Compiler will check printf-style format strings
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
}
// Return value checking
__attribute__((warn_unused_result))
bool criticalOperation() {
// Return value should not be ignored
return true;
}
// Sentinel attribute for variadic functions
__attribute__((sentinel))
void processStrings(const char* first, ...) {
// Last argument must be NULL
va_list args;
va_start(args, first);
const char* current = first;
while (current != nullptr) {
std::cout << current << " ";
current = va_arg(args, const char*);
}
va_end(args);
std::cout << std::endl;
}
// Microsoft Visual C++ specific attributes
#ifdef _MSC_VER
__declspec(noinline)
void msvcNoInline() {
std::cout << "MSVC no-inline function" << std::endl;
}
__declspec(forceinline)
inline void msvcForceInline() {
// Force inlining even if compiler thinks it's not beneficial
}
__declspec(dllexport)
void exportedFunction() {
std::cout << "Function exported from DLL" << std::endl;
}
__declspec(align(16))
struct AlignedStruct {
double data[2];
};
#endif
// Mixed standard and compiler-specific attributes
[[nodiscard]] __attribute__((pure))
int combinedAttributesFunction(int x, int y) {
return x * y;
}
[[deprecated("Use modernCombinedFunction()")]] __attribute__((cold))
void legacyCombinedFunction() {
std::cout << "Legacy function with mixed attributes" << std::endl;
}
// Class with compiler-specific attributes
class __attribute__((packed)) PackedClass {
char a;
int b; // Normally would be aligned, but packed prevents padding
char c;
};
// Attribute on class methods
class AttributedClass {
public:
__attribute__((always_inline))
inline int inlinedMethod() const { return value_; }
__attribute__((noinline))
void nonInlinedMethod() {
value_++;
}
[[nodiscard]] __attribute__((pure))
int pureMethod(int x) const {
return value_ + x;
}
private:
mutable int value_ = 0;
};
// Function demonstrating compiler-specific attribute usage
void demonstrateCompilerAttributes() {
// Test always_inline/noinline
int fast = fastFunction(5);
slowFunction();
// Test pure/const functions
int pureResult = pureCalculation(3, 4);
int constResult = constantCalculation(6);
// Test hot/cold functions
frequentlyCalledFunction(); // Called in hot path
try {
throw std::runtime_error("Test error");
} catch (...) {
rarelyCalledFunction(); // Called in error path (cold)
}
// Test visibility functions
publicApiFunction();
internalFunction();
// Test weak symbols
weakFunction(); // May call overridden version if available
// Test deprecated function
char buffer[100];
insecureFunction(buffer); // Warning: deprecated
std::string secureBuffer;
newSecureFunction(secureBuffer); // Modern alternative
// Test format checking
debugPrintf("Debug value: %d, string: %s\\n", 42, "test");
// Test return value checking
bool result = criticalOperation(); // OK
criticalOperation(); // Warning: unused result
// Test sentinel function
processStrings("first", "second", "third", nullptr); // Must end with nullptr
// Test combined attributes
int combined = combinedAttributesFunction(7, 8); // nodiscard + pure
legacyCombinedFunction(); // deprecated + cold
// Test class attributes
PackedClass packed;
std::cout << "PackedClass size: " << sizeof(PackedClass) << std::endl;
AttributedClass attributed;
int inlined = attributed.inlinedMethod(); // always_inline
attributed.nonInlinedMethod(); // noinline
int pure = attributed.pureMethod(10); // nodiscard + pure
#ifdef _MSC_VER
// Test MSVC-specific attributes
msvcNoInline();
msvcForceInline();
exportedFunction();
AlignedStruct aligned;
std::cout << "AlignedStruct alignment: " << alignof(AlignedStruct) << std::endl;
#endif
}
// Template function with attributes
template<typename T>
__attribute__((always_inline)) [[nodiscard]]
constexpr T attributedTemplate(T value) noexcept {
return value * 2;
}
// Specialization with different attributes
template<>
__attribute__((noinline)) [[nodiscard]]
int attributedTemplate<int>(int value) noexcept {
// Force no-inline for int specialization
return value * 3;
}
void testAttributedTemplates() {
auto floatResult = attributedTemplate<float>(2.5f); // Inlined
auto intResult = attributedTemplate<int>(5); // Not inlined
// Test discarding results
attributedTemplate<double>(3.0); // Warning: nodiscard
}
// Attribute inheritance in class hierarchies
class __attribute__((visibility("default"))) BaseClass {
public:
virtual ~BaseClass() = default;
[[nodiscard]] virtual bool virtualMethod() const = 0;
__attribute__((hot))
void hotMethod() const {
// Frequently called base method
}
};
class DerivedClass : public BaseClass {
public:
[[nodiscard]] bool virtualMethod() const override {
return true;
}
__attribute__((cold))
void coldMethod() const {
// Rarely called derived method
}
};
void testAttributeInheritance() {
DerivedClass derived;
bool result = derived.virtualMethod(); // nodiscard from override
derived.hotMethod(); // Inherited hot method
derived.coldMethod(); // Cold derived method
// Polymorphic call
BaseClass* base = &derived;
bool polyResult = base->virtualMethod(); // Virtual dispatch with nodiscard
base->hotMethod(); // Virtual call to hot method
}
""",
)
run_updater(cpp_attributes_project, mock_ingestor)
all_relationships = cast(
MagicMock, mock_ingestor.ensure_relationship_batch
).call_args_list
defines_relationships = get_relationships(mock_ingestor, "DEFINES")
[c for c in all_relationships if c.args[1] == "CALLS"]
compiler_attributed_functions = [
call
for call in defines_relationships
if "compiler_attributes" in call.args[0][2]
and any(
func in call.args[2][2]
for func in [
"fastFunction",
"slowFunction",
"pureCalculation",
"constantCalculation",
"frequentlyCalledFunction",
"rarelyCalledFunction",
"publicApiFunction",
]
)
]
assert len(compiler_attributed_functions) >= 6, (
f"Expected at least 6 compiler-attributed functions, found {len(compiler_attributed_functions)}"
)
attributed_classes = [
call
for call in defines_relationships
if "compiler_attributes" in call.args[0][2]
and any(
cls in call.args[2][2]
for cls in ["PackedClass", "AttributedClass", "BaseClass", "DerivedClass"]
)
]
assert len(attributed_classes) >= 3, (
f"Expected at least 3 attributed classes, found {len(attributed_classes)}"
)
def test_attribute_combinations_and_edge_cases(
cpp_attributes_project: Path,
mock_ingestor: MagicMock,
) -> None:
"""Test complex attribute combinations and edge cases."""
test_file = cpp_attributes_project / "attribute_edge_cases.cpp"
test_file.write_text(
encoding="utf-8",
data="""
#include <iostream>
#include <functional>
#include <memory>
#include <type_traits>
// Multiple standard attributes on same declaration
[[nodiscard, deprecated("Use newMultiAttributeFunction() instead"), maybe_unused]]
int multiAttributeFunction(int x) {
return x * 2;
}
// Attributes on different parts of function signature
[[nodiscard]]
auto complexAttributeFunction(
[[maybe_unused]] int param1,
const std::string& param2
) noexcept [[deprecated("Complex signature deprecated")]] -> int {
return static_cast<int>(param2.length());
}
// Attributes in template contexts
template<typename T>
[[nodiscard]] constexpr bool templateWithAttributes(
[[maybe_unused]] T&& value,
[[maybe_unused]] int context = 0
) noexcept {
if constexpr (std::is_integral_v<T>) {
return true;
} else [[likely]] {
return false;
}
}
// Conditional attributes based on compilation context
#ifdef DEBUG
#define DEBUG_DEPRECATED [[deprecated("Debug-only function")]]
#define DEBUG_MAYBE_UNUSED [[maybe_unused]]
#else
#define DEBUG_DEPRECATED
#define DEBUG_MAYBE_UNUSED
#endif
DEBUG_DEPRECATED
void debugOnlyFunction(DEBUG_MAYBE_UNUSED int debugLevel) {
std::cout << "Debug function" << std::endl;
}
// Attributes on lambda expressions
void testLambdaAttributes() {
// Lambda with attributes on parameters
auto lambda1 = [](
[[maybe_unused]] int unused_param,
int used_param
) [[nodiscard]] -> int {
return used_param * 2;
};
// Lambda with attributes on capture
int captured = 42;
auto lambda2 = [captured]
[[deprecated("Use lambda3 instead")]]
[[nodiscard]] (int x) -> int {
return captured + x;
};
// Use lambdas
int result1 = lambda1(0, 5); // maybe_unused param, nodiscard result
int result2 = lambda2(10); // deprecated + nodiscard
lambda1(1, 2); // Warning: discarding nodiscard result
}
// Attributes on structured bindings (C++17)
void testStructuredBindingAttributes() {
std::pair<int, std::string> data{42, "test"};
// Attributes on structured binding declaration
[[maybe_unused]] auto [value, text] = data;
// Only use one binding
std::cout << "Text: " << text << std::endl;
// 'value' is maybe_unused, so no warning
}
// Custom attribute-like macros
#define FORCE_INLINE __attribute__((always_inline)) inline
#define NO_RETURN [[noreturn]]
#define PURE_FUNCTION [[nodiscard]] __attribute__((pure))
FORCE_INLINE
int customInlineFunction(int x) {
return x + 1;
}
NO_RETURN
void terminateProgram() {
std::terminate();
}
PURE_FUNCTION
int customPureFunction(int a, int b) {
return a * b + 1;
}
// Attributes on member function pointers
class AttributePointerTest {
public:
[[nodiscard]] int normalMethod() const { return 42; }
[[deprecated("Use newMethod() instead")]]
void oldMethod() const {}
void newMethod() const {}
};
void testMemberFunctionPointers() {
AttributePointerTest obj;
// Pointer to attributed member function
auto normalPtr = &AttributePointerTest::normalMethod;
auto oldPtr = &AttributePointerTest::oldMethod;
auto newPtr = &AttributePointerTest::newMethod;
// Call through pointers
int result = (obj.*normalPtr)(); // nodiscard
(obj.*oldPtr)(); // deprecated
(obj.*newPtr)(); // normal
// Discard nodiscard result
(obj.*normalPtr)(); // Warning: discarding nodiscard
}
// Attributes on friend functions
class FriendAttributeTest {
friend [[deprecated("Friend function deprecated")]]
void deprecatedFriend(const FriendAttributeTest& obj) {
std::cout << "Deprecated friend: " << obj.value_ << std::endl;
}
friend [[nodiscard]]
bool modernFriend(const FriendAttributeTest& obj) {
return obj.value_ > 0;
}
private:
int value_ = 100;
};
void testFriendAttributes() {
FriendAttributeTest obj;
deprecatedFriend(obj); // Warning: deprecated
bool result = modernFriend(obj); // nodiscard
modernFriend(obj); // Warning: discarding nodiscard
}
// Attributes on type aliases and using declarations
[[deprecated("Use ModernType instead")]]
using LegacyType = int;
using ModernType = int;
[[maybe_unused]]
using OptionalType = std::optional<int>;
// Attributes on variable templates
template<typename T>
[[maybe_unused]] constexpr bool is_special_v = false;
template<>
constexpr bool is_special_v<int> = true;
// Attributes on concepts (C++20)
#if __cpp_concepts >= 201907L
template<typename T>
[[deprecated("Use modern_concept instead")]]
concept legacy_concept = std::is_integral_v<T>;
template<typename T>
concept modern_concept = std::is_arithmetic_v<T>;
#endif
// Function with attributes on exception specification
[[nodiscard]]
int riskyFunction() noexcept(false) [[deprecated("May throw exceptions")]] {
if (rand() % 2) {
throw std::runtime_error("Random error");
}
return 42;
}
// Nested attribute scenarios
namespace [[deprecated("Legacy namespace")]] legacy {
[[deprecated("Nested deprecated function")]]
void nestedDeprecated() {
std::cout << "Doubly deprecated" << std::endl;
}
class [[deprecated("Nested deprecated class")]] NestedClass {
public:
[[deprecated("Triply nested deprecated method")]]
void method() const {
std::cout << "Triply deprecated" << std::endl;
}
};
}
// Comprehensive demonstration function
void demonstrateAttributeEdgeCases() {
// Test multi-attribute function
int multi = multiAttributeFunction(5); // deprecated + nodiscard
multiAttributeFunction(10); // Warning: discarding nodiscard
// Test complex signature attributes
int complex = complexAttributeFunction(0, "test"); // deprecated + nodiscard
// Test template attributes
bool intResult = templateWithAttributes(42); // likely branch
bool stringResult = templateWithAttributes(std::string("test")); // unlikely branch
// Test debug attributes
debugOnlyFunction(3); // May be deprecated in debug builds
// Test lambda attributes
testLambdaAttributes();
// Test structured bindings
testStructuredBindingAttributes();
// Test custom attribute macros
int inlined = customInlineFunction(5);
int pure = customPureFunction(3, 4);
// terminateProgram(); // Would terminate - commented out
// Test member function pointer attributes
testMemberFunctionPointers();
// Test friend function attributes
testFriendAttributes();
// Test type alias attributes
LegacyType legacy = 42; // Warning: deprecated type
ModernType modern = 24; // OK
// Test risky function
try {
int risky = riskyFunction(); // deprecated + nodiscard + may throw
std::cout << "Risky result: " << risky << std::endl;
} catch (const std::exception& e) {
std::cerr << "Caught: " << e.what() << std::endl;
}
// Test nested deprecations
legacy::nestedDeprecated(); // Deprecated namespace + function
legacy::NestedClass nested; // Deprecated namespace + class
nested.method(); // Deprecated namespace + class + method
#if __cpp_concepts >= 201907L
// Test concept attributes
static_assert(legacy_concept<int>); // Warning: deprecated concept
static_assert(modern_concept<int>); // OK
#endif
}
// Final comprehensive test with all attribute types
[[nodiscard]] [[deprecated("Ultimate test function")]]
__attribute__((cold)) __attribute__((warn_unused_result))
bool ultimateAttributeTest(
[[maybe_unused]] int param1,
const std::string& param2
) noexcept [[likely]] {