Skip to content

A7-1-2: Do not report function candidates for constexpr #844

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions change_notes/2025-01-21-a7-1-2-remove-function-constexpr.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- `A7-1-2` - `FunctionMissingConstexpr.ql`
- Address false positives by removing the query - the rule is not intended to cover functions.
160 changes: 0 additions & 160 deletions cpp/autosar/src/rules/A7-1-2/FunctionMissingConstexpr.ql

This file was deleted.

16 changes: 0 additions & 16 deletions cpp/autosar/test/rules/A7-1-2/FunctionMissingConstexpr.expected

This file was deleted.

This file was deleted.

27 changes: 14 additions & 13 deletions cpp/autosar/test/rules/A7-1-2/VariableMissingConstexpr.expected
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@
| test.cpp:44:16:44:17 | lc | Variable 'lc' could be marked 'constexpr'. |
| test.cpp:45:17:45:19 | lc2 | Variable 'lc2' could be marked 'constexpr'. |
| test.cpp:55:7:55:8 | m2 | Variable 'm2' could be marked 'constexpr' and static. |
| test.cpp:130:7:130:8 | m1 | Variable 'm1' could be marked 'constexpr' and static. |
| test.cpp:141:7:141:8 | m1 | Variable 'm1' could be marked 'constexpr' and static. |
| test.cpp:221:7:221:8 | l1 | Variable 'l1' could be marked 'constexpr'. |
| test.cpp:235:7:235:8 | l6 | Variable 'l6' could be marked 'constexpr'. |
| test.cpp:237:7:237:8 | l8 | Variable 'l8' could be marked 'constexpr'. |
| test.cpp:240:7:240:9 | l10 | Variable 'l10' could be marked 'constexpr'. |
| test.cpp:243:7:243:9 | l12 | Variable 'l12' could be marked 'constexpr'. |
| test.cpp:248:7:248:9 | l15 | Variable 'l15' could be marked 'constexpr'. |
| test.cpp:250:7:250:9 | l16 | Variable 'l16' could be marked 'constexpr'. |
| test.cpp:251:7:251:9 | l17 | Variable 'l17' could be marked 'constexpr'. |
| test.cpp:257:7:257:9 | l21 | Variable 'l21' could be marked 'constexpr'. |
| test.cpp:262:7:262:9 | l24 | Variable 'l24' could be marked 'constexpr'. |
| test.cpp:263:7:263:9 | l25 | Variable 'l25' could be marked 'constexpr'. |
| test.cpp:65:7:65:8 | x2 | Variable 'x2' could be marked 'constexpr'. |
| test.cpp:66:13:66:14 | x3 | Variable 'x3' could be marked 'constexpr'. |
| test.cpp:76:7:76:8 | m1 | Variable 'm1' could be marked 'constexpr' and static. |
| test.cpp:91:7:91:8 | l1 | Variable 'l1' could be marked 'constexpr'. |
| test.cpp:105:7:105:8 | l6 | Variable 'l6' could be marked 'constexpr'. |
| test.cpp:107:7:107:8 | l8 | Variable 'l8' could be marked 'constexpr'. |
| test.cpp:110:7:110:9 | l10 | Variable 'l10' could be marked 'constexpr'. |
| test.cpp:113:7:113:9 | l12 | Variable 'l12' could be marked 'constexpr'. |
| test.cpp:118:7:118:9 | l15 | Variable 'l15' could be marked 'constexpr'. |
| test.cpp:120:7:120:9 | l16 | Variable 'l16' could be marked 'constexpr'. |
| test.cpp:121:7:121:9 | l17 | Variable 'l17' could be marked 'constexpr'. |
| test.cpp:127:7:127:9 | l21 | Variable 'l21' could be marked 'constexpr'. |
| test.cpp:132:7:132:9 | l24 | Variable 'l24' could be marked 'constexpr'. |
| test.cpp:133:7:133:9 | l25 | Variable 'l25' could be marked 'constexpr'. |
144 changes: 7 additions & 137 deletions cpp/autosar/test/rules/A7-1-2/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,71 +56,17 @@ class MemberConstExpr {
int m3 = 0; // COMPLIANT - can be set by constructor
};

int h1(int x, int y) { // NON_COMPLIANT
return x + y;
}

constexpr int h1_correct(int x, int y) { // COMPLIANT
return x + y;
}

int h2(int x) { return h1(x, 1) + 1; } // NON_COMPLIANT
constexpr int h2_correct(int x) { return h1_correct(x, 1) + 1; } // COMPLIANT

int h3(int x) { // COMPLIANT - uses goto, so can't be constexpr
if (x) {
goto l1;
} else {
return 10;
}
l1:
return 1;
}

int h4(int x) { // COMPLIANT - uses try, so can't be constexpr
try {
return 1;
} catch (...) {
}
}

int h5(int x) { // COMPLIANT - declares non literal local var
NonLiteralClass nlc;
}

int h6(int x) { // COMPLIANT - declares static variable
static int i = x;
return x;
}

int h7(int x) { // COMPLIANT - declares no init variable
int i;
}
int h1(int x, int y) { return x + y; }

int h8(int x) { // NON_COMPLIANT - could be constexpr
int i = x;
return i;
}
constexpr int h1_const(int x, int y) { return x + y; }

constexpr int h8_correct(int x) { // COMPLIANT
int i = x;
return i;
int h2() {
int x1 = h1(1, 1); // COMPLIANT
int x2 = h1_const(1, 1); // NON_COMPLIANT
const int x3 = h1_const(1, 1); // NON_COMPLIANT
constexpr int x4 = h1_const(1, 1); // COMPLIANT
}

int h9(int x) { // COMPLIANT - declares thread local variable
thread_local int i = x;
return x;
}

class ConstexprFunctionClass {
public:
int mf1(int x) { return m1 + x; } // NON_COMPLIANT
constexpr int mf1_correct(int x) { return m1 + x; } // COMPLIANT

private:
int m1;
};

class MissingConstexprClass {
public:
MissingConstexprClass() = default; // NON_COMPLIANT
Expand All @@ -130,82 +76,6 @@ class MissingConstexprClass {
int m1 = 0; // NON_COMPLIANT
};

class VirtualBaseClass {};

class DerivedClass : public virtual VirtualBaseClass {
public:
DerivedClass() = default; // COMPLIANT
DerivedClass(int i) = delete; // COMPLIANT
DerivedClass(int i, LiteralClass lc) {} // COMPLIANT
private:
int m1 = 0; // NON_COMPLIANT
};

class NotAllMembersInitializedClass {
public:
NotAllMembersInitializedClass() = default; // COMPLIANT
NotAllMembersInitializedClass(int i) = delete; // COMPLIANT
NotAllMembersInitializedClass(int i, LiteralClass lc) {} // COMPLIANT
private:
int m1;
};

class NonLiteralParamsClass {
public:
NonLiteralParamsClass(int i, NonLiteralClass lc) {} // COMPLIANT
};

// Variant members are always initialized, so this can be marked constexpr
class VariantMemberInitialized {
public:
VariantMemberInitialized() = default; // NON_COMPLIANT
VariantMemberInitialized(int i) = delete; // NON_COMPLIANT
VariantMemberInitialized(int i, LiteralClass lc) {} // NON_COMPLIANT
private:
union {
int i = 0;
short s;
};
};

class VariantMemberInitConstexpr {
public:
constexpr VariantMemberInitConstexpr() = default; // COMPLIANT
constexpr VariantMemberInitConstexpr(int i) = delete; // COMPLIANT
constexpr VariantMemberInitConstexpr(int i, LiteralClass lc) {} // COMPLIANT
private:
union {
int i = 0;
short s;
};
};

// Variant members are not initialized at declaration, so we can only mark the
// constructors as constexpr if we explicitly initialize the variant member
class VariantMemberNotInit {
public:
VariantMemberNotInit() = default; // COMPLIANT
VariantMemberNotInit(int pi) = delete; // COMPLIANT
VariantMemberNotInit(int pi, LiteralClass lc) {} // COMPLIANT
VariantMemberNotInit(LiteralClass lc, int pi) : i(pi) {} // NON_COMPLIANT
constexpr VariantMemberNotInit(LiteralClass lc, short pi) // COMPLIANT
: i(pi) {}

private:
union {
int i;
short s;
};
};

class ExcludedCases {
public:
~ExcludedCases() {} // COMPLIANT

void operator=(ExcludedCases &) {} // COMPLIANT
void operator=(ExcludedCases &&) {} // COMPLIANT
};

extern int random();
constexpr int add(int x, int y) { return x + y; }
// Example with compile time constant literal value as default argument
Expand Down
Loading
Loading