Skip to content
Merged
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
12 changes: 6 additions & 6 deletions cpp/autosar/src/rules/M3-1-2/FunctionsDeclaredAtBlockScope.ql
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@

import cpp
import codingstandards.cpp.autosar
import codingstandards.cpp.rules.functiondeclaredatblockscope.FunctionDeclaredAtBlockScope

from DeclStmt decl, Function f
where
not isExcluded(decl, DeclarationsPackage::functionsDeclaredAtBlockScopeQuery()) and
not isExcluded(f, DeclarationsPackage::functionsDeclaredAtBlockScopeQuery()) and
decl.getADeclaration() = f
select f, "Function " + f.getName() + " is declared at block scope."
module FunctionDeclaredAtBlockScopeConfig implements FunctionDeclaredAtBlockScopeConfigSig {
Query getQuery() { result = DeclarationsPackage::functionsDeclaredAtBlockScopeQuery() }
}

import FunctionDeclaredAtBlockScope<FunctionDeclaredAtBlockScopeConfig>

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cpp/common/test/rules/functiondeclaredatblockscope/FunctionDeclaredAtBlockScope.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/
import cpp
import RuleMetadata
import codingstandards.cpp.exclusions.RuleMetadata

newtype Declarations3Query =
TVariableDeclaredArrayTypeQuery() or
TBlockScopeFunctionAmbiguousQuery()

predicate isDeclarations3QueryMetadata(Query query, string queryId, string ruleId, string category) {
query =
// `Query` instance for the `variableDeclaredArrayType` query
Declarations3Package::variableDeclaredArrayTypeQuery() and
queryId =
// `@id` for the `variableDeclaredArrayType` query
"cpp/misra/variable-declared-array-type" and
ruleId = "RULE-11-3-1" and
category = "advisory"
or
query =
// `Query` instance for the `blockScopeFunctionAmbiguous` query
Declarations3Package::blockScopeFunctionAmbiguousQuery() and
queryId =
// `@id` for the `blockScopeFunctionAmbiguous` query
"cpp/misra/block-scope-function-ambiguous" and
ruleId = "RULE-6-0-1" and
category = "required"
}

module Declarations3Package {
Query variableDeclaredArrayTypeQuery() {
//autogenerate `Query` type
result =
// `Query` type for `variableDeclaredArrayType` query
TQueryCPP(TDeclarations3PackageQuery(TVariableDeclaredArrayTypeQuery()))
}

Query blockScopeFunctionAmbiguousQuery() {
//autogenerate `Query` type
result =
// `Query` type for `blockScopeFunctionAmbiguous` query
TQueryCPP(TDeclarations3PackageQuery(TBlockScopeFunctionAmbiguousQuery()))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import DeadCode9
import Declarations
import Declarations1
import Declarations2
import Declarations3
import ExceptionSafety
import Exceptions1
import Exceptions2
Expand Down Expand Up @@ -131,6 +132,7 @@ newtype TCPPQuery =
TDeclarationsPackageQuery(DeclarationsQuery q) or
TDeclarations1PackageQuery(Declarations1Query q) or
TDeclarations2PackageQuery(Declarations2Query q) or
TDeclarations3PackageQuery(Declarations3Query q) or
TExceptionSafetyPackageQuery(ExceptionSafetyQuery q) or
TExceptions1PackageQuery(Exceptions1Query q) or
TExceptions2PackageQuery(Exceptions2Query q) or
Expand Down Expand Up @@ -228,6 +230,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId, string cat
isDeclarationsQueryMetadata(query, queryId, ruleId, category) or
isDeclarations1QueryMetadata(query, queryId, ruleId, category) or
isDeclarations2QueryMetadata(query, queryId, ruleId, category) or
isDeclarations3QueryMetadata(query, queryId, ruleId, category) or
isExceptionSafetyQueryMetadata(query, queryId, ruleId, category) or
isExceptions1QueryMetadata(query, queryId, ruleId, category) or
isExceptions2QueryMetadata(query, queryId, ruleId, category) or
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Provides a configurable module FunctionDeclaredAtBlockScope with a `problems` predicate
* for the following issue:
* A function declared at block scope can make code harder to read and may lead to
* developer confusion.
*/

import cpp
import codingstandards.cpp.Customizations
import codingstandards.cpp.Exclusions

signature module FunctionDeclaredAtBlockScopeConfigSig {
Query getQuery();
}

module FunctionDeclaredAtBlockScope<FunctionDeclaredAtBlockScopeConfigSig Config> {
query predicate problems(Function f, string message) {
exists(DeclStmt decl |
not isExcluded(decl, Config::getQuery()) and
not isExcluded(f, Config::getQuery()) and
decl.getADeclaration() = f and
message = "Function " + f.getName() + " is declared at block scope."
)
}
}
7 changes: 7 additions & 0 deletions cpp/common/test/includes/standard-library/string_view
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ typedef basic_string_view<wchar_t> wstring_view;
typedef basic_string_view<char16_t> u16string_view;
typedef basic_string_view<char32_t> u32string_view;

inline namespace literals {
inline namespace string_view_literals {
// suffix for basic_string_view literals
constexpr string_view operator""sv(const char *str, size_t len) noexcept;
} // namespace string_view_literals
} // namespace literals

} // namespace std

#endif // _GHLIBCPP_STRING_VIEW
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// GENERATED FILE - DO NOT MODIFY
import codingstandards.cpp.rules.functiondeclaredatblockscope.FunctionDeclaredAtBlockScope

module TestFileConfig implements FunctionDeclaredAtBlockScopeConfigSig {
Query getQuery() { result instanceof TestQuery }
}

import FunctionDeclaredAtBlockScope<TestFileConfig>
35 changes: 35 additions & 0 deletions cpp/misra/src/rules/RULE-11-3-1/VariableDeclaredArrayType.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* @id cpp/misra/variable-declared-array-type
* @name RULE-11-3-1: Variables of array type should not be declared
* @description Using array type instead of container types can lead to difficulty manually managing
* size and accesses.
* @kind problem
* @precision very-high
* @problem.severity error
* @tags external/misra/id/rule-11-3-1
* correctness
* maintainability
* readability
* scope/single-translation-unit
* external/misra/enforcement/decidable
* external/misra/obligation/advisory
*/

import cpp
import codingstandards.cpp.misra

class FixedSizeCharArray extends Variable {
FixedSizeCharArray() {
this.isStatic() and
this.getUnspecifiedType().stripType() instanceof CharType and
this.getInitializer().getExpr() instanceof StringLiteral
}
}

from Variable v
where
not isExcluded(v, Declarations3Package::variableDeclaredArrayTypeQuery()) and
exists(ArrayType a | v.getType() = a | not v instanceof FixedSizeCharArray) and
// Exclude the compiler generated __func__ as it is the only way to access the function name information
not v.getName() = "__func__"
select v, "Variable " + v.getName() + " has an array type."
25 changes: 25 additions & 0 deletions cpp/misra/src/rules/RULE-6-0-1/BlockScopeFunctionAmbiguous.ql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* @id cpp/misra/block-scope-function-ambiguous
* @name RULE-6-0-1: Block scope declarations shall not be visually ambiguous
* @description A function declared at block scope can make code harder to read and may lead to
* developer confusion.
* @kind problem
* @precision very-high
* @problem.severity warning
* @tags external/misra/id/rule-6-0-1
* maintainability
* readability
* scope/single-translation-unit
* external/misra/enforcement/decidable
* external/misra/obligation/required
*/

import cpp
import codingstandards.cpp.misra
import codingstandards.cpp.rules.functiondeclaredatblockscope.FunctionDeclaredAtBlockScope

module BlockScopeFunctionAmbiguousConfig implements FunctionDeclaredAtBlockScopeConfigSig {
Query getQuery() { result = Declarations3Package::blockScopeFunctionAmbiguousQuery() }
}

import FunctionDeclaredAtBlockScope<BlockScopeFunctionAmbiguousConfig>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
| test.cpp:5:5:5:5 | g | Variable g has an array type. |
| test.cpp:9:24:9:24 | a | Variable a has an array type. |
| test.cpp:10:7:10:8 | a1 | Variable a1 has an array type. |
| test.cpp:14:7:14:8 | a1 | Variable a1 has an array type. |
| test.cpp:19:7:19:7 | x | Variable x has an array type. |
| test.cpp:20:17:20:17 | a | Variable a has an array type. |
| test.cpp:22:8:22:9 | x1 | Variable x1 has an array type. |
| test.cpp:28:6:28:7 | g1 | Variable g1 has an array type. |
| test.cpp:34:21:34:21 | p | Variable p has an array type. |
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rules/RULE-11-3-1/VariableDeclaredArrayType.ql
36 changes: 36 additions & 0 deletions cpp/misra/test/rules/RULE-11-3-1/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

#include <array>
#include <string_view>

int g[100]; // NON_COMPLIANT

class C {
public:
static constexpr int a[1]{0}; // NON_COMPLIANT
int a1[1]; // NON_COMPLIANT
};

struct S {
int a1[1]; // NON_COMPLIANT
};

void test_c_arrays() {

int x[100]; // NON_COMPLIANT
constexpr int a[]{0, 1, 2}; // NON_COMPLIANT
const size_t s{1};
char x1[s]; // NON_COMPLIANT
std::array<char, s> x2; // COMPLIANT

__func__; // COMPLIANT
}

char g1[] = "abc"; // NON_COMPLIANT
const char g2[] = "abc"; // COMPLIANT

using namespace std::literals;
const auto g3 = "abc"sv; // COMPLIANT

void param_test(int p[1], int (&p1)[1], int (*p2)[1],
int *p3) { // NON_COMPLIANT -- p, rest are compliant
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cpp/common/test/rules/functiondeclaredatblockscope/FunctionDeclaredAtBlockScope.ql
1 change: 1 addition & 0 deletions rule_packages/cpp/Declarations.json
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@
"precision": "very-high",
"severity": "warning",
"short_name": "FunctionsDeclaredAtBlockScope",
"shared_implementation_short_name": "FunctionDeclaredAtBlockScope",
"tags": [
"correctness",
"maintainability"
Expand Down
53 changes: 53 additions & 0 deletions rule_packages/cpp/Declarations3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"MISRA-C++-2023": {
"RULE-11-3-1": {
"properties": {
"enforcement": "decidable",
"obligation": "advisory"
},
"queries": [
{
"description": "Using array type instead of container types can lead to difficulty manually managing size and accesses.",
"kind": "problem",
"name": "Variables of array type should not be declared",
"precision": "very-high",
"severity": "error",
"short_name": "VariableDeclaredArrayType",
"tags": [
"correctness",
"maintainability",
"readability",
"scope/single-translation-unit"
]
}
],
"title": "Variables of array type should not be declared"
},
"RULE-6-0-1": {
"properties": {
"enforcement": "decidable",
"obligation": "required"
},
"queries": [
{
"description": "A function declared at block scope can make code harder to read and may lead to developer confusion.",
"kind": "problem",
"name": "Block scope declarations shall not be visually ambiguous",
"precision": "very-high",
"severity": "warning",
"short_name": "BlockScopeFunctionAmbiguous",
"shared_implementation_short_name": "FunctionDeclaredAtBlockScope",
"tags": [
"maintainability",
"readability",
"scope/single-translation-unit"
],
"implementation_scope": {
"description": "The rule checks for functions in block scope only. The information CodeQL has access to in the database is not enough to cover the object with redundant parentheses case."
}
}
],
"title": "Block scope declarations shall not be visually ambiguous"
}
}
}
14 changes: 7 additions & 7 deletions rules.csv
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ cpp,MISRA-C++-2023,RULE-6-0-3,Yes,Advisory,Decidable,Single Translation Unit,"Th
cpp,MISRA-C++-2023,RULE-6-0-4,Yes,Required,Decidable,Single Translation Unit,The identifier main shall not be used for a function other than the global function main,M7-3-2,ImportMisra23,Import,
cpp,MISRA-C++-2023,RULE-6-2-1,Yes,Required,Decidable,System,The one-definition rule shall not be violated,M3-2-2,ImportMisra23,Import,
cpp,MISRA-C++-2023,RULE-6-2-2,Yes,Required,Decidable,System,All declarations of a variable or function shall have the same type,"M3-9-1,DCL40-C",Declarations2,Easy,
cpp,MISRA-C++-2023,RULE-6-2-3,Yes,Required,Decidable,System,The source code used to implement an entity shall appear only once,,Declarations3,Medium,
cpp,MISRA-C++-2023,RULE-6-2-3,Yes,Required,Decidable,System,The source code used to implement an entity shall appear only once,,Declarations7,Medium,
cpp,MISRA-C++-2023,RULE-6-2-4,Yes,Required,Decidable,Single Translation Unit,A header file shall not contain definitions of functions or objects that are non-inline and have external linkage,A3-1-1,Linkage2,Import,
cpp,MISRA-C++-2023,RULE-6-4-1,Yes,Required,Decidable,Single Translation Unit,A variable declared in an inner scope shall not hide a variable declared in an outer scope,A2-10-1,ImportMisra23,Import,
cpp,MISRA-C++-2023,RULE-6-4-2,Yes,Required,Decidable,Single Translation Unit,Derived classes shall not conceal functions that are inherited from their bases,A7-3-1,ImportMisra23,Import,
Expand All @@ -867,8 +867,8 @@ cpp,MISRA-C++-2023,RULE-6-7-2,Yes,Required,Decidable,Single Translation Unit,Glo
cpp,MISRA-C++-2023,RULE-6-8-1,Yes,Required,Undecidable,System,An object shall not be accessed outside of its lifetime,A3-8-1,ImportMisra23,Import,
cpp,MISRA-C++-2023,RULE-6-8-2,Yes,Mandatory,Decidable,Single Translation Unit,A function must not return a reference or a pointer to a local variable with automatic storage duration,M7-5-1,ImportMisra23,Import,
cpp,MISRA-C++-2023,RULE-6-8-3,Yes,Required,Decidable,Single Translation Unit,An assignment operator shall not assign the address of an object with automatic storage duration to an object with a greater lifetime,,Lifetime,Medium,
cpp,MISRA-C++-2023,RULE-6-8-4,Yes,Advisory,Decidable,Single Translation Unit,Member functions returning references to their object should be refqualified appropriately,,Declarations3,Medium,
cpp,MISRA-C++-2023,RULE-6-9-1,Yes,Required,Decidable,Single Translation Unit,The same type aliases shall be used in all declarations of the same entity,,Declarations3,Medium,
cpp,MISRA-C++-2023,RULE-6-8-4,Yes,Advisory,Decidable,Single Translation Unit,Member functions returning references to their object should be refqualified appropriately,,Declarations5,Medium,
cpp,MISRA-C++-2023,RULE-6-9-1,Yes,Required,Decidable,Single Translation Unit,The same type aliases shall be used in all declarations of the same entity,,Declarations5,Medium,
cpp,MISRA-C++-2023,RULE-6-9-2,Yes,Advisory,Decidable,Single Translation Unit,The names of the standard signed integer types and standard unsigned integer types should not be used,A3-9-1,BannedAPIs,Easy,
cpp,MISRA-C++-2023,RULE-7-0-1,Yes,Required,Decidable,Single Translation Unit,There shall be no conversion from type bool,,Conversions,Easy,
cpp,MISRA-C++-2023,RULE-7-0-2,Yes,Required,Decidable,Single Translation Unit,There shall be no conversion to type bool,,Conversions,Easy,
Expand Down Expand Up @@ -915,16 +915,16 @@ cpp,MISRA-C++-2023,RULE-9-6-3,Yes,Required,Decidable,Single Translation Unit,The
cpp,MISRA-C++-2023,RULE-9-6-4,Yes,Required,Undecidable,System,A function declared with the [[noreturn]] attribute shall not return,MSC53-CPP,ImportMisra23,Import,
cpp,MISRA-C++-2023,RULE-9-6-5,Yes,Required,Decidable,Single Translation Unit,A function with non-void return type shall return a value on all paths,MSC52-CPP,ImportMisra23,Import,
cpp,MISRA-C++-2023,RULE-10-0-1,Yes,Advisory,Decidable,Single Translation Unit,A declaration should not declare more than one variable or member variable,M8-0-1,ImportMisra23,Import,
cpp,MISRA-C++-2023,RULE-10-1-1,Yes,Advisory,Decidable,Single Translation Unit,The target type of a pointer or lvalue reference parameter should be const-qualified appropriately,RULE-8-13,Declarations3,Hard,
cpp,MISRA-C++-2023,RULE-10-1-2,Yes,Required,Decidable,Single Translation Unit,The volatile qualifier shall be used appropriately,,Declarations3,Easy,
cpp,MISRA-C++-2023,RULE-10-1-1,Yes,Advisory,Decidable,Single Translation Unit,The target type of a pointer or lvalue reference parameter should be const-qualified appropriately,RULE-8-13,Declarations6,Hard,
cpp,MISRA-C++-2023,RULE-10-1-2,Yes,Required,Decidable,Single Translation Unit,The volatile qualifier shall be used appropriately,,Declarations4,Easy,
cpp,MISRA-C++-2023,RULE-10-2-1,Yes,Required,Decidable,Single Translation Unit,An enumeration shall be defined with an explicit underlying type,A7-2-2,ImportMisra23,Import,
cpp,MISRA-C++-2023,RULE-10-2-2,Yes,Advisory,Decidable,Single Translation Unit,Unscoped enumerations should not be declared,A7-2-3,Banned2,Easy,
cpp,MISRA-C++-2023,RULE-10-2-3,Yes,Required,Decidable,Single Translation Unit,The numeric value of an unscoped enumeration with no fixed underlying type shall not be used,A4-5-1,Banned3,Easy,
cpp,MISRA-C++-2023,RULE-10-3-1,Yes,Advisory,Decidable,Single Translation Unit,There should be no unnamed namespaces in header files,"DCL59-CPP, M7-3-3",Banned4,Easy,
cpp,MISRA-C++-2023,RULE-10-4-1,Yes,Required,Decidable,Single Translation Unit,The asm declaration shall not be used,A7-4-1,ImportMisra23,Import,
cpp,MISRA-C++-2023,RULE-11-3-1,Yes,Advisory,Decidable,Single Translation Unit,Variables of array type should not be declared,,Declarations3,Easy,
cpp,MISRA-C++-2023,RULE-11-3-1,Yes,Advisory,Decidable,Single Translation Unit,Variables of array type should not be declared,A18-1-1,Declarations3,Easy,
cpp,MISRA-C++-2023,RULE-11-3-2,Yes,Advisory,Decidable,Single Translation Unit,The declaration of an object should contain no more than two levels of pointer indirection,A5-0-3,ImportMisra23,Import,
cpp,MISRA-C++-2023,RULE-11-6-1,Yes,Advisory,Decidable,Single Translation Unit,All variables should be initialized,,Declarations3,Easy,
cpp,MISRA-C++-2023,RULE-11-6-1,Yes,Advisory,Decidable,Single Translation Unit,All variables should be initialized,,Declarations7,Easy,
cpp,MISRA-C++-2023,RULE-11-6-2,Yes,Mandatory,Undecidable,System,The value of an object must not be read before it has been set,A8-5-0,Lifetime,Import
cpp,MISRA-C++-2023,RULE-11-6-3,Yes,Required,Decidable,Single Translation Unit,"Within an enumerator list, the value of an implicitly-specified enumeration constant shall be unique",RULE-8-12,ImportMisra23,Import,
cpp,MISRA-C++-2023,RULE-12-2-1,Yes,Advisory,Decidable,Single Translation Unit,Bit-fields should not be declared,A9-6-2,Banned,Easy,
Expand Down
Loading