Skip to content
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

A2-7-3: Exclude declarations in function scopes #452

Merged
merged 5 commits into from
Feb 15, 2024
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
3 changes: 3 additions & 0 deletions change_notes/2023-11-24-a2-7-3-remove-function-scope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* `A2-7-3` - `UndocumentedUserDefinedType.ql`:
- Excluding declarations in function scope. The rationale is that these declarations are not exposed outside the scope of the function.

15 changes: 11 additions & 4 deletions cpp/autosar/src/rules/A2-7-3/UndocumentedUserDefinedType.ql
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
import cpp
import codingstandards.cpp.autosar

private predicate isInFunctionScope(Declaration d) {
// Type declared in function
exists(d.(UserType).getEnclosingFunction())
or
// Member declared in type which is in function scope
isInFunctionScope(d.getDeclaringType())
}

/**
* A declaration which is required to be preceded by documentation by AUTOSAR A2-7-3.
*/
Expand All @@ -42,10 +50,8 @@ class DocumentableDeclaration extends Declaration {
declarationType = "member variable" and
// Exclude memeber variables in instantiated templates, which cannot reasonably be documented.
not this.(MemberVariable).isFromTemplateInstantiation(_) and
// Exclude anonymous lambda functions.
// TODO: replace with the following when support is added.
// not this.(MemberVariable).isCompilerGenerated()
not exists(LambdaExpression lc | lc.getACapture().getField() = this)
// Exclude compiler generated variables, such as those for anonymous lambda functions
not this.(MemberVariable).isCompilerGenerated()
}

/** Gets a `DeclarationEntry` for this declaration that should be documented. */
Expand Down Expand Up @@ -96,6 +102,7 @@ from DocumentableDeclaration d, DeclarationEntry de
where
not isExcluded(de, CommentsPackage::undocumentedUserDefinedTypeQuery()) and
not isExcluded(d, CommentsPackage::undocumentedUserDefinedTypeQuery()) and
not isInFunctionScope(d) and
d.getAnUndocumentedDeclarationEntry() = de
select de,
"Declaration entry for " + d.getDeclarationType() + " " + d.getName() +
Expand Down
17 changes: 16 additions & 1 deletion cpp/autosar/test/rules/A2-7-3/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,19 @@ template <typename T> class A2_7_3 final {
const std::string kBar{"bar"}; // NON_COMPLIANT
};
/// @brief This is the instantiateA2_7_3 documentation
void instantiateA2_7_3() { A2_7_3<int> instance; }
void instantiateA2_7_3() { A2_7_3<int> instance; }

/// Test documentation
void testFunctionScope() {
using my_float = float;
class ClassF { // COMPLIANT - in function scope
public:
int m_x; // COMPLIANT - in function scope
void fTest(); // COMPLIANT - in function scope
class ClassFNested {
public:
int m_nested_x; // COMPLIANT - in function scope
void fNestedTest(); // COMPLIANT - in function scope
};
};
}
5 changes: 4 additions & 1 deletion rule_packages/cpp/Comments.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@
"tags": [
"maintainability",
"readability"
]
],
"implementation_scope": {
"description": "Function scope declarations are excluded from this rule as they are restricted in scope to only a single function."
}
}
],
"title": "All declarations of 'user-defined' types, static and non-static data members, functions and methods shall be preceded by documentation."
Expand Down
Loading