Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

A2-10-5: Exclude variables in template instantiations #64

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 5 commits into from
Sep 30, 2022
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 2 change_notes/2022-08-18-variable-templates-reused-name.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- `A2-10-5` - `IdentifierNameOfANonMemberObjectWithExternalOrInternalLinkageIsReused.ql`
- Reduce false positives by excluding variable template instantiations.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ where
not isExcluded(o2,
NamingPackage::identifierNameOfANonMemberObjectWithExternalOrInternalLinkageIsReusedQuery()) and
not o1 = o2 and
o1.getName() = o2.getName()
o1.getName() = o2.getName() and
// Only consider variables from uninstantiated templates, to avoid false positives where o1 and
// o2 are the same object across different template instantiations
not o1.isFromTemplateInstantiation(_) and
not o2.isFromTemplateInstantiation(_)
select o2,
"Identifier name of non-member object $@ reuses the identifier name of non-member object $@.", o2,
o2.getName(), o1, o1.getName()
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
| test1a.cpp:6:12:6:13 | g3 | Identifier name of non-member object $@ reuses the identifier name of non-member object $@. | test1a.cpp:6:12:6:13 | g3 | g3 | test1b.cpp:7:12:7:13 | g3 | g3 |
| test1a.cpp:17:43:17:43 | number_two | Identifier name of non-member object $@ reuses the identifier name of non-member object $@. | test1a.cpp:17:43:17:43 | number_two | number_two | test1b.cpp:12:43:12:43 | number_two | number_two |
| test1b.cpp:7:12:7:13 | g3 | Identifier name of non-member object $@ reuses the identifier name of non-member object $@. | test1b.cpp:7:12:7:13 | g3 | g3 | test1a.cpp:6:12:6:13 | g3 | g3 |
| test1b.cpp:12:43:12:43 | number_two | Identifier name of non-member object $@ reuses the identifier name of non-member object $@. | test1b.cpp:12:43:12:43 | number_two | number_two | test1a.cpp:17:43:17:43 | number_two | number_two |
14 changes: 14 additions & 0 deletions 14 cpp/autosar/test/rules/A2-10-5/test1a.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,17 @@ static int g1 = 0;
static int g2; // COMPLIANT
static int g3 = 1; // NON_COMPLIANT
static void f1(){}; // NON_COMPLIANT

// Variable template has multiple declarations: one for the uninstantiated
// template and one for each instantiation
template <class T> constexpr T number_one = T(1); // COMPLIANT

int test() { return number_one<int>; }

long test2() { return number_one<long>; }

template <class T> constexpr T number_two = T(1); // NON_COMPLIANT

int test3() { return number_two<int>; }

long test4() { return number_two<long>; }
6 changes: 6 additions & 0 deletions 6 cpp/autosar/test/rules/A2-10-5/test1b.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ static int g3 = 0; // NON_COMPLIANT
}

static void f1() {} // NON_COMPLIANT

template <class T> constexpr T number_two = T(1); // NON_COMPLIANT

int test3() { return number_two<int>; }

long test4() { return number_two<long>; }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.