Skip to content

Commit 6f91612

Browse files
committed
Declarations6: add RULE-17-3
and refactor Identifiers to include new predicate
1 parent 4acb620 commit 6f91612

File tree

11 files changed

+148
-9
lines changed

11 files changed

+148
-9
lines changed

.vscode/tasks.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@
206206
"Declarations",
207207
"Declarations1",
208208
"Declarations2",
209+
"Declarations6",
209210
"Exceptions1",
210211
"Exceptions2",
211212
"Expressions",
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+
from FunctionDeclarationEntry fde
20+
where
21+
not isExcluded(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+
select fde, "Function declaration is implicit."
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test.c:3:1:3:2 | declaration of f2 | Function declaration is implicit. |
2+
| test.c:11:17:11:17 | declaration of f3 | Function declaration is implicit. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-17-3/FunctionDeclaredImplicitly.ql

c/misra/test/rules/RULE-17-3/test.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// semmle-extractor-options:--clang -std=c11 -nostdinc
2+
// -I../../../../common/test/includes/standard-library
3+
double f1(double x); // COMPLIANT
4+
f2(double x); // NON_COMPLIANT
5+
6+
void f() {
7+
double l = 1;
8+
double l1 = f1(l);
9+
10+
double l2 = f2(l);
11+
12+
double l3 = f3(l); // NON_COMPLIANT
13+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import cpp
2+
import codingstandards.cpp.Linkage
3+
4+
class ExternalIdentifiers extends InterestingIdentifiers {
5+
ExternalIdentifiers() {
6+
hasExternalLinkage(this) and
7+
getNamespace() instanceof GlobalNamespace
8+
}
9+
10+
string getSignificantName() {
11+
//C99 states the first 31 characters of external identifiers are significant
12+
//C90 states the first 6 characters of external identifiers are significant and case is not required to be significant
13+
//C90 is not currently considered by this rule
14+
result = this.getName().prefix(31)
15+
}
16+
}
17+
18+
//Identifiers that are candidates for checking uniqueness
19+
class InterestingIdentifiers extends Declaration {
20+
InterestingIdentifiers() {
21+
not this.isFromTemplateInstantiation(_) and
22+
not this.isFromUninstantiatedTemplate(_) and
23+
not this instanceof TemplateParameter and
24+
not this.hasDeclaringType() and
25+
not this instanceof Operator and
26+
not this.hasName("main") and
27+
exists(this.getADeclarationLocation())
28+
}
29+
30+
//this definition of significant relies on the number of significant characters for a macro name (C99)
31+
//this is used on macro name comparisons only
32+
//not necessarily against other types of identifiers
33+
string getSignificantNameComparedToMacro() { result = this.getName().prefix(63) }
34+
}
35+
36+
//Declarations that omit type - C90 compiler assumes int
37+
predicate isDeclaredImplicit(Declaration d) {
38+
d.hasSpecifier("implicit_int") and
39+
exists(Type t |
40+
(d.(Variable).getType() = t or d.(Function).getType() = t) and
41+
// Exclude "short" or "long", as opposed to "short int" or "long int".
42+
t instanceof IntType and
43+
// Exclude "signed" or "unsigned", as opposed to "signed int" or "unsigned int".
44+
not exists(IntegralType it | it = t | it.isExplicitlySigned() or it.isExplicitlyUnsigned())
45+
)
46+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/
2+
import cpp
3+
import RuleMetadata
4+
import codingstandards.cpp.exclusions.RuleMetadata
5+
6+
newtype Declarations6Query = TFunctionDeclaredImplicitlyQuery()
7+
8+
predicate isDeclarations6QueryMetadata(Query query, string queryId, string ruleId, string category) {
9+
query =
10+
// `Query` instance for the `functionDeclaredImplicitly` query
11+
Declarations6Package::functionDeclaredImplicitlyQuery() and
12+
queryId =
13+
// `@id` for the `functionDeclaredImplicitly` query
14+
"c/misra/function-declared-implicitly" and
15+
ruleId = "RULE-17-3" and
16+
category = "mandatory"
17+
}
18+
19+
module Declarations6Package {
20+
Query functionDeclaredImplicitlyQuery() {
21+
//autogenerate `Query` type
22+
result =
23+
// `Query` type for `functionDeclaredImplicitly` query
24+
TQueryC(TDeclarations6PackageQuery(TFunctionDeclaredImplicitlyQuery()))
25+
}
26+
}

cpp/common/src/codingstandards/cpp/exclusions/c/RuleMetadata.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import Contracts4
1515
import Declarations1
1616
import Declarations2
1717
import Declarations3
18+
import Declarations6
1819
import Expressions
1920
import IO1
2021
import IO2
@@ -53,6 +54,7 @@ newtype TCQuery =
5354
TDeclarations1PackageQuery(Declarations1Query q) or
5455
TDeclarations2PackageQuery(Declarations2Query q) or
5556
TDeclarations3PackageQuery(Declarations3Query q) or
57+
TDeclarations6PackageQuery(Declarations6Query q) or
5658
TExpressionsPackageQuery(ExpressionsQuery q) or
5759
TIO1PackageQuery(IO1Query q) or
5860
TIO2PackageQuery(IO2Query q) or
@@ -91,6 +93,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId, string cat
9193
isDeclarations1QueryMetadata(query, queryId, ruleId, category) or
9294
isDeclarations2QueryMetadata(query, queryId, ruleId, category) or
9395
isDeclarations3QueryMetadata(query, queryId, ruleId, category) or
96+
isDeclarations6QueryMetadata(query, queryId, ruleId, category) or
9497
isExpressionsQueryMetadata(query, queryId, ruleId, category) or
9598
isIO1QueryMetadata(query, queryId, ruleId, category) or
9699
isIO2QueryMetadata(query, queryId, ruleId, category) or

cpp/common/src/codingstandards/cpp/rules/typeomitted/TypeOmitted.qll

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,14 @@
55
import cpp
66
import codingstandards.cpp.Customizations
77
import codingstandards.cpp.Exclusions
8+
import codingstandards.cpp.Identifiers
89

910
abstract class TypeOmittedSharedQuery extends Query { }
1011

1112
Query getQuery() { result instanceof TypeOmittedSharedQuery }
1213

1314
query predicate problems(Declaration d, string message) {
1415
not isExcluded(d, getQuery()) and
15-
d.hasSpecifier("implicit_int") and
16-
exists(Type t |
17-
(d.(Variable).getType() = t or d.(Function).getType() = t) and
18-
// Exclude "short" or "long", as opposed to "short int" or "long int".
19-
t instanceof IntType and
20-
// Exclude "signed" or "unsigned", as opposed to "signed int" or "unsigned int".
21-
not exists(IntegralType it | it = t | it.isExplicitlySigned() or it.isExplicitlyUnsigned())
22-
) and
16+
isDeclaredImplicit(d) and
2317
message = "Declaration " + d.getName() + " is missing a type specifier."
2418
}

rule_packages/c/Declarations6.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"MISRA-C-2012": {
3+
"RULE-17-3": {
4+
"properties": {
5+
"obligation": "mandatory"
6+
},
7+
"queries": [
8+
{
9+
"description": "Omission of type specifiers may not be supported by some compilers. Additionally implicit typing can lead to undefined behaviour.",
10+
"kind": "problem",
11+
"name": "A function shall not be declared implicitly",
12+
"precision": "very-high",
13+
"severity": "error",
14+
"short_name": "FunctionDeclaredImplicitly",
15+
"tags": [
16+
"correctness",
17+
"readability"
18+
]
19+
}
20+
],
21+
"title": "A function shall not be declared implicitly"
22+
}
23+
}
24+
}

rules.csv

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ c,MISRA-C-2012,RULE-16-6,Yes,Required,,,Every switch statement shall have at lea
713713
c,MISRA-C-2012,RULE-16-7,Yes,Required,,,A switch-expression shall not have essentially Boolean type,M6-4-7,Statements,Medium,
714714
c,MISRA-C-2012,RULE-17-1,Yes,Required,,,The features of <stdarg.h> shall not be used,,Banned,Easy,
715715
c,MISRA-C-2012,RULE-17-2,Yes,Required,,,"Functions shall not call themselves, either directly or indirectly",A7-5-2,Statements,Import,
716-
c,MISRA-C-2012,RULE-17-3,Yes,Mandatory,,,A function shall not be declared implicitly,,Declarations,Medium,
716+
c,MISRA-C-2012,RULE-17-3,Yes,Mandatory,,,A function shall not be declared implicitly,,Declarations6,Medium,
717717
c,MISRA-C-2012,RULE-17-4,Yes,Mandatory,,,All exit paths from a function with non-void return type shall have an explicit return statement with an expression,MSC52-CPP,Statements,Medium,
718718
c,MISRA-C-2012,RULE-17-5,Yes,Advisory,,,The function argument corresponding to a parameter declared to have an array type shall have an appropriate number of elements,,Contracts,Hard,
719719
c,MISRA-C-2012,RULE-17-6,No,Mandatory,,,The declaration of an array parameter shall not contain the static keyword between the [ ],,,,

0 commit comments

Comments
 (0)