Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commite5fdac4

Browse files
authored
Merge pull request#157 from knewbury01/knewbury01/Declarations6
Implement C Declarations6 package
2 parentse37a13c +1e8eb59 commite5fdac4

File tree

41 files changed

+646
-34
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+646
-34
lines changed

‎.vscode/tasks.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@
209209
"Declarations3",
210210
"Declarations4",
211211
"Declarations5",
212+
"Declarations6",
212213
"Exceptions1",
213214
"Exceptions2",
214215
"Expressions",

‎c/cert/src/rules/DCL38-C/DeclaringAFlexibleArrayMember.ql‎

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,12 @@
1515

1616
import cpp
1717
import codingstandards.c.cert
18-
19-
/**
20-
* A member with the type array that is last in a struct
21-
* includes any sized array (either specified or not)
22-
*/
23-
classFlexibleArrayMemberextendsMemberVariable{
24-
Structs;
25-
26-
FlexibleArrayMember(){
27-
this.getType()instanceofArrayTypeand
28-
this.getDeclaringType()=sand
29-
notexists(inti,intj|
30-
s.getAMember(i)=thisand
31-
exists(s.getAMember(j))and
32-
j>i
33-
)
34-
}
35-
}
18+
import codingstandards.c.Variable
3619

3720
fromVariableDeclarationEntrym,ArrayTypea
3821
where
3922
notisExcluded(m, Declarations2Package::declaringAFlexibleArrayMemberQuery())and
4023
m.getType()=aand
41-
m.getVariable()instanceofFlexibleArrayMemberand
24+
m.getVariable()instanceofFlexibleArrayMemberCandidateand
4225
a.getArraySize()=1
4326
selectm,"Incorrect syntax used for declaring this flexible array member."

‎c/common/src/codingstandards/c/Variable.qll‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,35 @@ class VlaVariable extends Variable {
66
/* Extractor workaround do determine if a VLA array has the specifier volatile.*/
77
overridepredicateisVolatile(){this.getType().(ArrayType).getBaseType().isVolatile()}
88
}
9+
10+
/**
11+
* A flexible array member
12+
* ie member with the type array that is last in a struct
13+
* has no size specified
14+
*/
15+
classFlexibleArrayMemberextendsFlexibleArrayMemberCandidate{
16+
FlexibleArrayMember(){
17+
exists(ArrayTypet|
18+
this.getType()=tand
19+
notexists(t.getSize())
20+
)
21+
}
22+
}
23+
24+
/**
25+
* A member with the type array that is last in a struct
26+
* includes any sized array (either specified or not)
27+
*/
28+
classFlexibleArrayMemberCandidateextendsMemberVariable{
29+
Structs;
30+
31+
FlexibleArrayMemberCandidate(){
32+
this.getType()instanceofArrayTypeand
33+
this.getDeclaringType()=sand
34+
notexists(inti,intj|
35+
s.getAMember(i)=thisand
36+
exists(s.getAMember(j))and
37+
j>i
38+
)
39+
}
40+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @id c/misra/function-declared-implicitly
3+
* @name RULE-17-3: A function shall not be declared implicitly
4+
* @description Omission of type specifiers may not be supported by some compilers. Additionally
5+
* implicit typing can lead to undefined behaviour.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity error
9+
* @tags external/misra/id/rule-17-3
10+
* correctness
11+
* readability
12+
* external/misra/obligation/mandatory
13+
*/
14+
15+
import cpp
16+
import codingstandards.c.misra
17+
import codingstandards.cpp.Identifiers
18+
19+
fromFunctionDeclarationEntryfde
20+
where
21+
notisExcluded(fde, Declarations6Package::functionDeclaredImplicitlyQuery())and
22+
(
23+
//use before declaration
24+
fde.isImplicit()
25+
or
26+
//declared but type not explicit
27+
isDeclaredImplicit(fde.getDeclaration())
28+
)
29+
selectfde,"Function declaration is implicit."
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @id c/misra/flexible-array-members-declared
3+
* @name RULE-18-7: Flexible array members shall not be declared
4+
* @description The use of flexible array members can lead to unexpected program behaviour.
5+
* @kind problem
6+
* @precision very-high
7+
* @problem.severity error
8+
* @tags external/misra/id/rule-18-7
9+
* correctness
10+
* external/misra/obligation/required
11+
*/
12+
13+
import cpp
14+
import codingstandards.c.misra
15+
import codingstandards.c.Variable
16+
17+
fromFlexibleArrayMemberf
18+
wherenotisExcluded(f, Declarations6Package::flexibleArrayMembersDeclaredQuery())
19+
selectf,"Flexible array member declared."
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @id c/misra/identifiers-with-external-linkage-not-unique
3+
* @name RULE-5-8: Identifiers that define objects or functions with external linkage shall be unique
4+
* @description Using non-unique identifiers can lead to developer confusion.
5+
* @kind problem
6+
* @precision very-high
7+
* @problem.severity error
8+
* @tags external/misra/id/rule-5-8
9+
* maintainability
10+
* readability
11+
* external/misra/obligation/required
12+
*/
13+
14+
import cpp
15+
import codingstandards.c.misra
16+
import codingstandards.cpp.Identifiers
17+
18+
fromDeclarationde,ExternalIdentifierse
19+
where
20+
notisExcluded(de, Declarations6Package::identifiersWithExternalLinkageNotUniqueQuery())and
21+
notisExcluded(e, Declarations6Package::identifiersWithExternalLinkageNotUniqueQuery())and
22+
notde=eand
23+
de.getName()=e.getName()
24+
selectde,"Identifier conflicts with external identifier $@",e,e.getName()
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @id c/misra/identifiers-with-internal-linkage-not-unique
3+
* @name RULE-5-9: Identifiers that define objects or functions with internal linkage should be unique
4+
* @description Using non-unique identifiers can lead to developer confusion.
5+
* @kind problem
6+
* @precision very-high
7+
* @problem.severity error
8+
* @tags external/misra/id/rule-5-9
9+
* maintainability
10+
* readability
11+
* external/misra/obligation/advisory
12+
*/
13+
14+
import cpp
15+
import codingstandards.c.misra
16+
17+
fromDeclarationd1,Declarationd2
18+
where
19+
notisExcluded(d1, Declarations6Package::identifiersWithInternalLinkageNotUniqueQuery())and
20+
notisExcluded(d2, Declarations6Package::identifiersWithInternalLinkageNotUniqueQuery())and
21+
d1.isStatic()and
22+
d1.isTopLevel()and
23+
notd1=d2and
24+
d1.getName()=d2.getName()and
25+
// Apply an ordering based on location to enforce that (d1, d2) = (d2, d1) and we only report (d1, d2).
26+
(
27+
d1.getFile().getAbsolutePath()<d2.getFile().getAbsolutePath()
28+
or
29+
d1.getFile().getAbsolutePath()=d2.getFile().getAbsolutePath()and
30+
d1.getLocation().getStartLine()<d2.getLocation().getStartLine()
31+
)
32+
selectd2,"Identifier conflicts with identifier $@ with internal linkage.",d1,d1.getName()
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @id c/misra/inline-function-not-declared-static-storage
3+
* @name RULE-8-10: An inline function shall be declared with the static storage class
4+
* @description Declaring an inline function with external linkage can lead to undefined or
5+
* incorrect program behaviour.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity error
9+
* @tags external/misra/id/rule-8-10
10+
* correctness
11+
* external/misra/obligation/required
12+
*/
13+
14+
import cpp
15+
import codingstandards.c.misra
16+
import codingstandards.cpp.Identifiers
17+
18+
fromFunctionDeclarationEntryf
19+
where
20+
notisExcluded(f, Declarations6Package::inlineFunctionNotDeclaredStaticStorageQuery())and
21+
f.getFunction()instanceofInterestingIdentifiersand
22+
f.getFunction().isInline()and
23+
notf.hasSpecifier("static")
24+
selectf,"Inline function not explicitly declared static."
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @id c/misra/array-external-linkage-size-explicitly-specified
3+
* @name RULE-8-11: When an array with external linkage is declared, its size should be explicitly specified
4+
* @description Declaring an array without an explicit size disallows the compiler and static
5+
* checkers from doing array bounds analysis and can lead to less readable, unsafe
6+
* code.
7+
* @kind problem
8+
* @precision very-high
9+
* @problem.severity error
10+
* @tags external/misra/id/rule-8-11
11+
* correctness
12+
* readability
13+
* external/misra/obligation/advisory
14+
*/
15+
16+
import cpp
17+
import codingstandards.c.misra
18+
import codingstandards.cpp.Identifiers
19+
20+
fromVariableDeclarationEntryv,ArrayTypet
21+
where
22+
notisExcluded(v, Declarations6Package::arrayExternalLinkageSizeExplicitlySpecifiedQuery())and
23+
v.getDeclaration()instanceofExternalIdentifiersand
24+
v.getType()=tand
25+
notexists(t.getSize())and
26+
//this rule applies to non-defining declarations only
27+
notv.isDefinition()
28+
selectv,"Array declared without explicit size."
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @id c/misra/should-not-be-defined-with-external-linkage
3+
* @name RULE-8-7: Functions and objects should not be defined with external linkage if they are referenced in only one
4+
* @description Declarations with external linkage that are referenced in only one translation unit
5+
* can indicate an intention to only have those identifiers accessible in that
6+
* translation unit and accidental future accesses in other translation units can lead
7+
* to confusing program behaviour.
8+
* @kind problem
9+
* @precision very-high
10+
* @problem.severity error
11+
* @tags external/misra/id/rule-8-7
12+
* correctness
13+
* maintainability
14+
* readability
15+
* external/misra/obligation/advisory
16+
*/
17+
18+
import cpp
19+
import codingstandards.c.misra
20+
import codingstandards.cpp.Identifiers
21+
import codingstandards.cpp.Scope
22+
23+
/**
24+
* Re-introduce function calls into access description as
25+
* "any reference"
26+
*/
27+
classReferenceextendsNameQualifiableElement{
28+
Reference(){
29+
thisinstanceofAccessor
30+
thisinstanceofFunctionCall
31+
}
32+
}
33+
34+
fromExternalIdentifierse,Referencea1,TranslationUnitt1
35+
where
36+
notisExcluded(e, Declarations6Package::shouldNotBeDefinedWithExternalLinkageQuery())and
37+
(a1.(Access).getTarget()=eora1.(FunctionCall).getTarget()=e)and
38+
a1.getFile()=t1and
39+
//not accessed in any other translation unit
40+
notexists(TranslationUnitt2,Referencea2|
41+
nott1=t2and
42+
(a2.(Access).getTarget()=eora2.(FunctionCall).getTarget()=e)and
43+
a2.getFile()=t2
44+
)
45+
selecte,"Declaration with external linkage is accessed in only one translation unit $@.",a1,
46+
a1.toString()

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp