Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- `RULE-5-10-1` - `PoorlyFormedIdentifier.ql`:
- Avoid false positives for none user defined identifiers like __PRETTY_FUNCTION__
15 changes: 14 additions & 1 deletion cpp/common/src/codingstandards/cpp/Identifiers.qll
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,20 @@ private module IdentifierIntroductionImpl {
not variable.isCompilerGenerated() and
// Some variables are not correctly marked as compiler generated, such as parameters of lambda
// conversion operators.
not variable.(Parameter).getFunction().isCompilerGenerated()
not variable.(Parameter).getFunction().isCompilerGenerated() and
// The compiler-provided predefined function identifiers (`__func__`, and the GCC/Clang
// extensions `__FUNCTION__` and `__PRETTY_FUNCTION__`) are synthesized once per enclosing
// function, but the extractor does not flag them as compiler generated, so they leak through
// the check above. They are not user-defined identifiers and should not be reported. Rather
// than hard-coding these names (which would wrongly suppress a genuine user declaration of
// such an identifier on a compiler that does not predefine it), we identify them
// structurally: they are `static` local variables with no definition (their only declaration
// entry is located at a use site), whereas a real `static` local always has an in-source
// definition.
not (
variable.(LocalVariable).isStatic() and
not variable.hasDefinition()
)
// Note: Some duplicate and/or unflagged compiler-generated variables seem to exist in tests,
// which may be related to constexpr in templates. These redundant variables seem to only be
// distinguishable by the fact that their start location is the same as their end location.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,5 @@
| test.cpp:184:7:184:15 | wctrans_t | Identifier 'wctrans_t' is a reserved name. |
| test.cpp:185:7:185:14 | wctype_t | Identifier 'wctype_t' is a reserved name. |
| test.cpp:186:7:186:12 | wint_t | Identifier 'wint_t' is a reserved name. |
| test.cpp:202:1:202:42 | __PRETTY_FUNCTION__ | Identifier '__PRETTY_FUNCTION__' contains double underscores. |
| test.cpp:202:1:202:42 | __PRETTY_FUNCTION__ | Identifier '__PRETTY_FUNCTION__' starts with underscore. |
15 changes: 15 additions & 0 deletions cpp/misra/test/rules/RULE-5-10-1/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,21 @@ void test_reserved_names() {
int wint_t = 20; // NON_COMPLIANT - reserved name
}

// Test case for compiler-predefined function identifiers (not user-defined).
// __func__ is standard; __FUNCTION__ and __PRETTY_FUNCTION__ are GCC/Clang
// extensions. They are synthesized per-function by the compiler, so despite
// containing double underscores / leading underscores they must not be flagged.
const char *test_predefined_function_identifiers() {
const char *a = __func__; // COMPLIANT - compiler-predefined, not user-defined
const char *b = __PRETTY_FUNCTION__; // COMPLIANT - compiler-predefined, not user-defined
return a ? a : b;
}

// Test case for a user-defined macro that reuses a compiler-predefined name.
// Redefining __PRETTY_FUNCTION__ as a macro is a user-defined identifier and
// should be flagged (double underscores + lowercase).
#define __PRETTY_FUNCTION__ "user_defined" // NON_COMPLIANT - user-defined macro

// Test case for valid identifiers
void test_valid_identifiers() {
int validName = 1; // COMPLIANT
Expand Down