Skip to content

Commit b321608

Browse files
committed
Implement RULE-6-5-1 as a shared rule with A3-3-1
1 parent 70375b4 commit b321608

File tree

14 files changed

+100
-45
lines changed

14 files changed

+100
-45
lines changed

cpp/autosar/src/rules/A3-3-1/ExternalLinkageNotDeclaredInHeaderFile.ql

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,10 @@
1717

1818
import cpp
1919
import codingstandards.cpp.autosar
20-
import codingstandards.cpp.Linkage
21-
import codingstandards.cpp.EncapsulatingFunctions
20+
import codingstandards.cpp.rules.externallinkagenotdeclaredinheaderfile.ExternalLinkageNotDeclaredInHeaderFile
2221

23-
from DeclarationEntry de, string kind
24-
where
25-
not isExcluded(de, IncludesPackage::externalLinkageNotDeclaredInHeaderFileQuery()) and
26-
hasExternalLinkage(de.getDeclaration()) and
27-
// Exclude subobjects such as struct members or member functions
28-
de.getDeclaration().isTopLevel() and
29-
// The declaration with external linkage does not have a declaration in a header file
30-
exists(Compilation c | de.getFile() = c.getAFileCompiled()) and
31-
not exists(DeclarationEntry otherDe |
32-
de.getDeclaration() = otherDe.getDeclaration() and
33-
not de = otherDe and
34-
not otherDe.isDefinition()
35-
|
36-
otherDe.getFile() instanceof HeaderFile
37-
) and
38-
// Main functions are an exception to the rule
39-
not de.getDeclaration() instanceof MainFunction and
40-
if de.getDeclaration() instanceof Function then kind = "function" else kind = "object"
41-
select de, "Externally linked " + kind + " '" + de.getName() + "' not declared in header file."
22+
class ExternalLinkageNotDeclaredInHeaderFileQuery extends ExternalLinkageNotDeclaredInHeaderFileSharedQuery {
23+
ExternalLinkageNotDeclaredInHeaderFileQuery() {
24+
this = IncludesPackage::externalLinkageNotDeclaredInHeaderFileQuery()
25+
}
26+
}

cpp/autosar/test/rules/A3-3-1/ExternalLinkageNotDeclaredInHeaderFile.qlref

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cpp/common/test/rules/externallinkagenotdeclaredinheaderfile/ExternalLinkageNotDeclaredInHeaderFile.ql

cpp/common/src/codingstandards/cpp/exclusions/cpp/Linkage1.qll

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,41 @@ import RuleMetadata
44
import codingstandards.cpp.exclusions.RuleMetadata
55

66
newtype Linkage1Query =
7-
TArrayExternalLinkageSizeExplicitQuery() or
8-
TExternalLinkageArrayWithoutExplicitSizeMisraQuery()
7+
TExternalLinkageArrayWithoutExplicitSizeMisraQuery() or
8+
TExternalLinkageNotDeclaredInHeaderFileMisraQuery()
99

1010
predicate isLinkage1QueryMetadata(Query query, string queryId, string ruleId, string category) {
1111
query =
12-
// `Query` instance for the `arrayExternalLinkageSizeExplicit` query
13-
Linkage1Package::arrayExternalLinkageSizeExplicitQuery() and
12+
// `Query` instance for the `externalLinkageArrayWithoutExplicitSizeMisra` query
13+
Linkage1Package::externalLinkageArrayWithoutExplicitSizeMisraQuery() and
1414
queryId =
15-
// `@id` for the `arrayExternalLinkageSizeExplicit` query
16-
"cpp/misra/array-external-linkage-size-explicit" and
15+
// `@id` for the `externalLinkageArrayWithoutExplicitSizeMisra` query
16+
"cpp/misra/external-linkage-array-without-explicit-size-misra" and
1717
ruleId = "RULE-6-0-2" and
1818
category = "advisory"
1919
or
2020
query =
21-
// `Query` instance for the `externalLinkageArrayWithoutExplicitSizeMisra` query
22-
Linkage1Package::externalLinkageArrayWithoutExplicitSizeMisraQuery() and
21+
// `Query` instance for the `externalLinkageNotDeclaredInHeaderFileMisra` query
22+
Linkage1Package::externalLinkageNotDeclaredInHeaderFileMisraQuery() and
2323
queryId =
24-
// `@id` for the `externalLinkageArrayWithoutExplicitSizeMisra` query
25-
"cpp/misra/external-linkage-array-without-explicit-size-misra" and
24+
// `@id` for the `externalLinkageNotDeclaredInHeaderFileMisra` query
25+
"cpp/misra/external-linkage-not-declared-in-header-file-misra" and
2626
ruleId = "RULE-6-5-1" and
2727
category = "advisory"
2828
}
2929

3030
module Linkage1Package {
31-
Query arrayExternalLinkageSizeExplicitQuery() {
31+
Query externalLinkageArrayWithoutExplicitSizeMisraQuery() {
3232
//autogenerate `Query` type
3333
result =
34-
// `Query` type for `arrayExternalLinkageSizeExplicit` query
35-
TQueryCPP(TLinkage1PackageQuery(TArrayExternalLinkageSizeExplicitQuery()))
34+
// `Query` type for `externalLinkageArrayWithoutExplicitSizeMisra` query
35+
TQueryCPP(TLinkage1PackageQuery(TExternalLinkageArrayWithoutExplicitSizeMisraQuery()))
3636
}
3737

38-
Query externalLinkageArrayWithoutExplicitSizeMisraQuery() {
38+
Query externalLinkageNotDeclaredInHeaderFileMisraQuery() {
3939
//autogenerate `Query` type
4040
result =
41-
// `Query` type for `externalLinkageArrayWithoutExplicitSizeMisra` query
42-
TQueryCPP(TLinkage1PackageQuery(TExternalLinkageArrayWithoutExplicitSizeMisraQuery()))
41+
// `Query` type for `externalLinkageNotDeclaredInHeaderFileMisra` query
42+
TQueryCPP(TLinkage1PackageQuery(TExternalLinkageNotDeclaredInHeaderFileMisraQuery()))
4343
}
4444
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Provides a library with a `problems` predicate for the following issue:
3+
* Using objects or functions with external linkage in implementation files makes code
4+
* harder to understand.
5+
*/
6+
7+
import cpp
8+
import codingstandards.cpp.Customizations
9+
import codingstandards.cpp.Exclusions
10+
import codingstandards.cpp.Linkage
11+
import codingstandards.cpp.EncapsulatingFunctions
12+
13+
abstract class ExternalLinkageNotDeclaredInHeaderFileSharedQuery extends Query { }
14+
15+
Query getQuery() { result instanceof ExternalLinkageNotDeclaredInHeaderFileSharedQuery }
16+
17+
query predicate problems(DeclarationEntry de, string message) {
18+
not isExcluded(de, getQuery()) and
19+
hasExternalLinkage(de.getDeclaration()) and
20+
// Exclude subobjects such as struct members or member functions
21+
de.getDeclaration().isTopLevel() and
22+
// The declaration with external linkage does not have a declaration in a header file
23+
exists(Compilation c | de.getFile() = c.getAFileCompiled()) and
24+
not exists(DeclarationEntry otherDe |
25+
de.getDeclaration() = otherDe.getDeclaration() and
26+
not de = otherDe and
27+
not otherDe.isDefinition()
28+
|
29+
otherDe.getFile() instanceof HeaderFile
30+
) and
31+
// Main functions are an exception to the rule
32+
not de.getDeclaration() instanceof MainFunction and
33+
if de.getDeclaration() instanceof Function
34+
then message = "Externally linked function '" + de.getName() + "' not declared in header file."
35+
else message = "Externally linked object '" + de.getName() + "' not declared in header file."
36+
}

cpp/autosar/test/rules/A3-3-1/ExternalLinkageNotDeclaredInHeaderFile.expected renamed to cpp/common/test/rules/externallinkagenotdeclaredinheaderfile/ExternalLinkageNotDeclaredInHeaderFile.expected

File renamed without changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// GENERATED FILE - DO NOT MODIFY
2+
import codingstandards.cpp.rules.externallinkagenotdeclaredinheaderfile.ExternalLinkageNotDeclaredInHeaderFile
3+
4+
class TestFileQuery extends ExternalLinkageNotDeclaredInHeaderFileSharedQuery, TestQuery { }

cpp/autosar/test/rules/A3-3-1/test.cpp renamed to cpp/common/test/rules/externallinkagenotdeclaredinheaderfile/test.cpp

File renamed without changes.

cpp/autosar/test/rules/A3-3-1/test.hpp renamed to cpp/common/test/rules/externallinkagenotdeclaredinheaderfile/test.hpp

File renamed without changes.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @id cpp/misra/external-linkage-not-declared-in-header-file-misra
3+
* @name RULE-6-5-1: Objects or functions with external linkage shall be declared in a header file
4+
* @description Using objects or functions with external linkage in implementation files makes code
5+
* harder to understand.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity warning
9+
* @tags external/misra/id/rule-6-5-1
10+
* correctness
11+
* maintainability
12+
* readability
13+
* scope/single-translation-unit
14+
* external/misra/enforcement/decidable
15+
* external/misra/obligation/advisory
16+
*/
17+
18+
import cpp
19+
import codingstandards.cpp.misra
20+
import codingstandards.cpp.rules.externallinkagenotdeclaredinheaderfile.ExternalLinkageNotDeclaredInHeaderFile
21+
22+
class ExternalLinkageNotDeclaredInHeaderFileMisraQuery extends ExternalLinkageNotDeclaredInHeaderFileSharedQuery {
23+
ExternalLinkageNotDeclaredInHeaderFileMisraQuery() {
24+
this = Linkage1Package::externalLinkageNotDeclaredInHeaderFileMisraQuery()
25+
}
26+
}

0 commit comments

Comments
 (0)