Skip to content

Commit 31f5ba4

Browse files
authored
Merge pull request #452 from github/a2-7-3-undocumented-function-scope
`A2-7-3`: Exclude declarations in function scopes
2 parents 6c7c258 + 4cffce3 commit 31f5ba4

File tree

4 files changed

+34
-6
lines changed

4 files changed

+34
-6
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
* `A2-7-3` - `UndocumentedUserDefinedType.ql`:
2+
- Excluding declarations in function scope. The rationale is that these declarations are not exposed outside the scope of the function.
3+

cpp/autosar/src/rules/A2-7-3/UndocumentedUserDefinedType.ql

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@
1717
import cpp
1818
import codingstandards.cpp.autosar
1919

20+
private predicate isInFunctionScope(Declaration d) {
21+
// Type declared in function
22+
exists(d.(UserType).getEnclosingFunction())
23+
or
24+
// Member declared in type which is in function scope
25+
isInFunctionScope(d.getDeclaringType())
26+
}
27+
2028
/**
2129
* A declaration which is required to be preceded by documentation by AUTOSAR A2-7-3.
2230
*/
@@ -42,10 +50,8 @@ class DocumentableDeclaration extends Declaration {
4250
declarationType = "member variable" and
4351
// Exclude memeber variables in instantiated templates, which cannot reasonably be documented.
4452
not this.(MemberVariable).isFromTemplateInstantiation(_) and
45-
// Exclude anonymous lambda functions.
46-
// TODO: replace with the following when support is added.
47-
// not this.(MemberVariable).isCompilerGenerated()
48-
not exists(LambdaExpression lc | lc.getACapture().getField() = this)
53+
// Exclude compiler generated variables, such as those for anonymous lambda functions
54+
not this.(MemberVariable).isCompilerGenerated()
4955
}
5056

5157
/** Gets a `DeclarationEntry` for this declaration that should be documented. */
@@ -96,6 +102,7 @@ from DocumentableDeclaration d, DeclarationEntry de
96102
where
97103
not isExcluded(de, CommentsPackage::undocumentedUserDefinedTypeQuery()) and
98104
not isExcluded(d, CommentsPackage::undocumentedUserDefinedTypeQuery()) and
105+
not isInFunctionScope(d) and
99106
d.getAnUndocumentedDeclarationEntry() = de
100107
select de,
101108
"Declaration entry for " + d.getDeclarationType() + " " + d.getName() +

cpp/autosar/test/rules/A2-7-3/test.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,19 @@ template <typename T> class A2_7_3 final {
160160
const std::string kBar{"bar"}; // NON_COMPLIANT
161161
};
162162
/// @brief This is the instantiateA2_7_3 documentation
163-
void instantiateA2_7_3() { A2_7_3<int> instance; }
163+
void instantiateA2_7_3() { A2_7_3<int> instance; }
164+
165+
/// Test documentation
166+
void testFunctionScope() {
167+
using my_float = float;
168+
class ClassF { // COMPLIANT - in function scope
169+
public:
170+
int m_x; // COMPLIANT - in function scope
171+
void fTest(); // COMPLIANT - in function scope
172+
class ClassFNested {
173+
public:
174+
int m_nested_x; // COMPLIANT - in function scope
175+
void fNestedTest(); // COMPLIANT - in function scope
176+
};
177+
};
178+
}

rule_packages/cpp/Comments.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@
7070
"tags": [
7171
"maintainability",
7272
"readability"
73-
]
73+
],
74+
"implementation_scope": {
75+
"description": "Function scope declarations are excluded from this rule as they are restricted in scope to only a single function."
76+
}
7477
}
7578
],
7679
"title": "All declarations of 'user-defined' types, static and non-static data members, functions and methods shall be preceded by documentation."

0 commit comments

Comments
 (0)