|
3 | 3 | */ |
4 | 4 |
|
5 | 5 | import cpp |
| 6 | +import codingstandards.cpp.CKeywords |
6 | 7 | import codingstandards.cpp.Customizations |
7 | 8 | import codingstandards.cpp.Exclusions |
8 | | -import codingstandards.cpp.Naming |
9 | | -import codingstandards.cpp.CKeywords |
| 9 | +import codingstandards.cpp.Linkage |
| 10 | +import codingstandards.cpp.Macro |
| 11 | +import codingstandards.cpp.StandardLibraryNames |
10 | 12 |
|
11 | 13 | abstract class DeclaredAReservedIdentifierSharedQuery extends Query { } |
12 | 14 |
|
13 | 15 | Query getQuery() { result instanceof DeclaredAReservedIdentifierSharedQuery } |
14 | 16 |
|
15 | | -query predicate problems(Element m, string message) { |
16 | | - not isExcluded(m, getQuery()) and |
17 | | - exists(string name | |
| 17 | +newtype Scope = |
| 18 | + FileScope() or |
| 19 | + BlockScope() or |
| 20 | + FunctionScope() or |
| 21 | + MacroScope() |
| 22 | + |
| 23 | +/** |
| 24 | + * A C name space according to C11 6.2.3, not to be confused with a C++ namespace. |
| 25 | + */ |
| 26 | +newtype TCNameSpace = |
| 27 | + LabelNameSpace() or |
| 28 | + TagNameSpace() or |
| 29 | + MemberNameSpace() or |
| 30 | + OrdinaryNameSpace() or |
| 31 | + // Create a "fake" name space for macro names |
| 32 | + MacroNameSpace() |
| 33 | + |
| 34 | +class CNameSpace extends TCNameSpace { |
| 35 | + string toString() { |
| 36 | + this = LabelNameSpace() and result = "label" |
| 37 | + or |
| 38 | + this = TagNameSpace() and result = "tag" |
| 39 | + or |
| 40 | + this = MemberNameSpace() and result = "member" |
| 41 | + or |
| 42 | + this = OrdinaryNameSpace() and result = "ordinary" |
| 43 | + or |
| 44 | + this = MacroNameSpace() and result = "macro" |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +string getDeclDescription(Declaration d) { |
| 49 | + d instanceof Function and result = "Function" |
| 50 | + or |
| 51 | + d instanceof Parameter and result = "Parameter" |
| 52 | + or |
| 53 | + d instanceof LocalVariable and result = "Local variable" |
| 54 | + or |
| 55 | + d instanceof MemberVariable and result = "Member variable" |
| 56 | + or |
| 57 | + d instanceof GlobalVariable and result = "Global variable" |
| 58 | + or |
| 59 | + d instanceof UserType and result = "Type" |
| 60 | + or |
| 61 | + d instanceof EnumConstant and result = "Enum constant" |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Whether a C declaration is considered to be at file scope. |
| 66 | + */ |
| 67 | +predicate isFileScope(Declaration d) { |
| 68 | + d.getFile().compiledAsC() and |
| 69 | + // Not inside a block - if the declaration is part of a DeclStmt, it is also part of a function |
| 70 | + // and therefore enclosed in at least one block |
| 71 | + not exists(DeclStmt ds | ds.getADeclaration() = d) and |
| 72 | + // Not inside a parameter list |
| 73 | + not d instanceof Parameter |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * The given C program element defines the `identifierName` in the given `Scope` and `CNameSpace`. |
| 78 | + */ |
| 79 | +predicate isCIdentifier( |
| 80 | + Element e, string identifierName, Scope scope, CNameSpace cNameSpace, string identifierDescription |
| 81 | +) { |
| 82 | + // This only makes sense on C compiled files |
| 83 | + e.getFile().compiledAsC() and |
| 84 | + ( |
| 85 | + // An identifier can denote an object; a function; a tag or a member of a structure, union, or |
| 86 | + // enumeration; a typedef name; a label name; a macro name; or a macro parameter |
| 87 | + e.(Declaration).hasName(identifierName) and |
| 88 | + // Exclude any compiler generated identifiers |
| 89 | + not e.(Variable).isCompilerGenerated() and |
| 90 | + not e.(Function).isCompilerGenerated() and |
| 91 | + // Exclude local variables generated by the compiler (but not marked as such by our extractor) |
| 92 | + not e.(LocalScopeVariable).hasName(["__func__", "__PRETTY_FUNCTION__", "__FUNCTION__"]) and |
| 93 | + // Exclude special "macro" |
| 94 | + ( |
| 95 | + if isFileScope(e) |
| 96 | + then |
| 97 | + // technically ignoring the function prototype scope, but we don't care for this use case |
| 98 | + scope = FileScope() |
| 99 | + else scope = BlockScope() |
| 100 | + ) and |
18 | 101 | ( |
19 | | - m.(Macro).hasName(name) or |
20 | | - m.(Declaration).hasGlobalName(name) |
| 102 | + if e instanceof UserType |
| 103 | + then cNameSpace = TagNameSpace() |
| 104 | + else |
| 105 | + if (e instanceof MemberVariable or e instanceof MemberFunction) |
| 106 | + then cNameSpace = MemberNameSpace() |
| 107 | + else cNameSpace = OrdinaryNameSpace() |
21 | 108 | ) and |
22 | 109 | ( |
23 | | - Naming::Cpp14::hasStandardLibraryMacroName(name) |
| 110 | + if exists(getDeclDescription(e)) |
| 111 | + then identifierDescription = getDeclDescription(e) |
| 112 | + else identifierDescription = "Identifier" |
| 113 | + ) |
| 114 | + or |
| 115 | + e.(LabelStmt).getName() = identifierName and |
| 116 | + scope = FunctionScope() and |
| 117 | + cNameSpace = LabelNameSpace() and |
| 118 | + identifierDescription = "Label" |
| 119 | + or |
| 120 | + e.(Macro).hasName(identifierName) and |
| 121 | + scope = MacroScope() and |
| 122 | + cNameSpace = MacroNameSpace() and |
| 123 | + identifierDescription = "Macro" |
| 124 | + or |
| 125 | + e.(FunctionLikeMacro).getAParameter() = identifierName and |
| 126 | + // Exclude __VA_ARGS__ as it is a special macro parameter |
| 127 | + not identifierName = "__VA_ARGS__..." and |
| 128 | + scope = MacroScope() and |
| 129 | + cNameSpace = MacroNameSpace() and |
| 130 | + identifierDescription = "Macro parameter" |
| 131 | + ) |
| 132 | +} |
| 133 | + |
| 134 | +module TargetedCLibrary = CStandardLibrary::C11; |
| 135 | + |
| 136 | +query predicate problems(Element m, string message) { |
| 137 | + not isExcluded(m, getQuery()) and |
| 138 | + exists( |
| 139 | + string name, Scope scope, CNameSpace cNameSpace, string reason, string identifierDescription |
| 140 | + | |
| 141 | + isCIdentifier(m, name, scope, cNameSpace, identifierDescription) and |
| 142 | + message = identifierDescription + " '" + name + "' " + reason + "." |
| 143 | + | |
| 144 | + // C11 7.1.3/1 |
| 145 | + // > All identifiers that begin with an underscore and either an uppercase letter or another |
| 146 | + // > underscore are always reserved for any use. |
| 147 | + name.regexpMatch("__.*") and |
| 148 | + // Exclude this macro which is intended to be implemented by the user |
| 149 | + not name = "__STDC_WANT_LIB_EXT1__" and |
| 150 | + reason = "uses a reserved name beginning with __" |
| 151 | + or |
| 152 | + name.regexpMatch("_[A-Z].*") and |
| 153 | + reason = "uses a reserved name beginning _ followed by an uppercase letter" |
| 154 | + or |
| 155 | + // > All identifiers that begin with an underscore are always reserved for use as identifiers |
| 156 | + // > with file scope in both the ordinary and tag name spaces. |
| 157 | + name.regexpMatch("_([^A-Z_].*)?") and |
| 158 | + scope = FileScope() and |
| 159 | + cNameSpace = [OrdinaryNameSpace().(TCNameSpace), TagNameSpace()] and |
| 160 | + reason = "uses a name beginning with _ which is reserved in the " + cNameSpace + " name space" |
| 161 | + or |
| 162 | + name.regexpMatch("_([^A-Z_].*)?") and |
| 163 | + scope = MacroScope() and |
| 164 | + cNameSpace = MacroNameSpace() and |
| 165 | + reason = "uses a name beginning with _ which is reserved in the ordinary and tag namespaces" |
| 166 | + or |
| 167 | + // > Each macro name in any of the following subclauses (including the future library |
| 168 | + // > directions) is reserved for use as specified if any of its associated headers is included; |
| 169 | + // > unless explicitly stated otherwise (see 7.1.4). |
| 170 | + exists(string header | |
| 171 | + TargetedCLibrary::hasMacroName(header, name, _) and |
| 172 | + reason = |
| 173 | + "uses a name reserved for a macro from the " + TargetedCLibrary::getName() + |
| 174 | + " standard library header '" + header + "'" |
| 175 | + ) |
| 176 | + or |
| 177 | + // > All identifiers with external linkage in any of the following subclauses (including the |
| 178 | + // > future library directions) and errno are always reserved for use as identifiers with |
| 179 | + // > external linkage.184 |
| 180 | + exists(string header | |
| 181 | + TargetedCLibrary::hasObjectName(header, _, name, _, "external") |
24 | 182 | or |
25 | | - Naming::Cpp14::hasStandardLibraryObjectName(name) |
| 183 | + TargetedCLibrary::hasFunctionName(header, _, "", name, _, _, "external") |
26 | 184 | or |
27 | | - Naming::Cpp14::hasStandardLibraryFunctionName(name) |
| 185 | + // > 184. The list of reserved identifiers with external linkage includes math_errhandling, setjmp, |
| 186 | + // > va_copy, and va_end. |
| 187 | + name = "errno" and |
| 188 | + header = "errno.h" |
28 | 189 | or |
29 | | - name.regexpMatch("_[A-Z_].*") |
| 190 | + name = "math_errhandling" and |
| 191 | + header = "math.h" |
30 | 192 | or |
31 | | - name.regexpMatch("_.*") and m.(Declaration).hasGlobalName(name) |
| 193 | + name = "setjmp" and |
| 194 | + header = "setjmp.h" |
32 | 195 | or |
33 | | - Keywords::isKeyword(name) |
34 | | - ) and |
35 | | - message = "Reserved identifier '" + name + "' is declared." |
| 196 | + name = "va_copy" and |
| 197 | + header = "stdarg.h" |
| 198 | + or |
| 199 | + name = "va_end" and |
| 200 | + header = "stdarg.h" |
| 201 | + | |
| 202 | + hasExternalLinkage(m) and |
| 203 | + reason = |
| 204 | + "uses a reserved name from the " + TargetedCLibrary::getName() + |
| 205 | + " standard library header '" + header + "'" |
| 206 | + ) |
| 207 | + or |
| 208 | + // > Each identifier with file scope listed in any of the following subclauses (including the |
| 209 | + // > future library directions) is reserved for use as a macro name and as an identifier with |
| 210 | + // > file scope in the same name space if any of its associated headers is included |
| 211 | + // Note: we do not consider the requirement of including the associated header |
| 212 | + exists(string header | |
| 213 | + TargetedCLibrary::hasObjectName(header, _, name, _, _) and |
| 214 | + (cNameSpace = OrdinaryNameSpace() or cNameSpace = MacroNameSpace()) |
| 215 | + or |
| 216 | + TargetedCLibrary::hasFunctionName(header, _, "", name, _, _, _) and |
| 217 | + (cNameSpace = OrdinaryNameSpace() or cNameSpace = MacroNameSpace()) |
| 218 | + or |
| 219 | + TargetedCLibrary::hasTypeName(header, _, name) and |
| 220 | + (cNameSpace = TagNameSpace() or cNameSpace = MacroNameSpace()) |
| 221 | + or |
| 222 | + TargetedCLibrary::hasMemberVariableName(header, _, _, name, _) and |
| 223 | + (cNameSpace = OrdinaryNameSpace() or cNameSpace = MacroNameSpace()) |
| 224 | + | |
| 225 | + ( |
| 226 | + scope = FileScope() |
| 227 | + or |
| 228 | + scope = MacroScope() |
| 229 | + ) and |
| 230 | + reason = |
| 231 | + "uses a reserved name from the " + TargetedCLibrary::getName() + |
| 232 | + " standard library header '" + header + "'" |
| 233 | + ) |
| 234 | + or |
| 235 | + // C11 6.4.1/2 |
| 236 | + Keywords::isKeyword(name) and |
| 237 | + reason = "it is a C11 keyword" |
36 | 238 | ) |
37 | 239 | } |
0 commit comments