Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
xjia1 committed Nov 16, 2023
1 parent d522db0 commit fdc66e5
Show file tree
Hide file tree
Showing 44 changed files with 4,630 additions and 81 deletions.
1 change: 0 additions & 1 deletion .bazeliskrc

This file was deleted.

4 changes: 2 additions & 2 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ build --copt=-Wall --copt=-Werror --host_copt=-Wall --host_copt=-Werror
build --copt=-Wno-unused --host_copt=-Wno-unused

# To build zlib
# TODO(xjia): remove if https://github.com/madler/zlib/issues/633 is resolved
# TODO: remove if https://github.com/madler/zlib/issues/633 is resolved
build --copt=-Wno-deprecated-non-prototype
build --host_copt=-Wno-deprecated-non-prototype
# TODO(xjia): remove the following two lines once all of us move to Clang 15+
# TODO: remove the following two lines once all of us move to Clang 15+
build --copt=-Wno-unknown-warning-option
build --host_copt=-Wno-unknown-warning-option
6 changes: 6 additions & 0 deletions analyzer/proto/results.proto
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ message Result {
MISRA_C_2012_RULE_1_1_IOM_ID_CHAR = 1010116;
MISRA_C_2012_RULE_1_1_NESTED_COND_INCLU = 1010117;
MISRA_C_2012_RULE_1_1_BLOCK_ID = 1010118;
MISRA_C_2012_RULE_1_1_NESTED_DECL = 1010119;
MISRA_C_2012_RULE_1_1_MODIFY_DECL = 1010120;
MISRA_C_2012_RULE_1_3 = 1010301;
MISRA_C_2012_RULE_1_4 = 1010400;
MISRA_C_2012_RULE_2_1 = 1020101;
Expand Down Expand Up @@ -552,6 +554,10 @@ message Result {
string nested_cond_inclu_count = 60;
string block_id_limit = 61;
string block_id_count = 62;
string nested_decl_limit = 63;
string nested_decl_count = 64;
string modify_decl_limit = 65;
string modify_decl_count = 66;

int32 severity = 21;
string code_line_hash = 22;
Expand Down
10 changes: 6 additions & 4 deletions cruleslib/basic/libtoolingargs.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,26 @@ const (
kIoMIDCharLimit string = "IoMIDCharLimit" // IoM: Internal or Macro
kNestedCondIncluLimit string = "NestedCondIncluLimit"
kBlockIDLimit string = "BlockIDLimit"
kNestedDeclLimit string = "NestedDeclLimit"
kModifyDeclLimit string = "ModifyDeclLimit"
)

var libtoolingExtraArgsMap = map[string][]string{
"misra_c_2012/rule_1_1": {kStructMemberLimit, kFunctionParmLimit, kFunctionArgLimit,
kNestedRecordLimit, kNestedExprLimit, kSwitchCaseLimit, kEnumConstantLimit,
kStringCharLimit, kExternIDLimit, kCaseSensitive, kLimit, kMacroIDLimit,
kMacroParmLimit, kMacroArgLimit, kNestedBlockLimit, kNestedIncludeLimit,
kImplicitDecl, kIoMIDCharLimit, kNestedCondIncluLimit, kBlockIDLimit},

kImplicitDecl, kIoMIDCharLimit, kNestedCondIncluLimit, kBlockIDLimit,
kNestedDeclLimit, kModifyDeclLimit},
"misra_c_2012/rule_5_1": {kCaseSensitive, kLimit, kImplicitDecl},
"misra_c_2012/rule_13_2": {kAggressiveMode},
"misra_c_2012/rule_13_5": {kAggressiveMode},
"misra/rule_1_1": {kStructMemberLimit, kFunctionParmLimit, kFunctionArgLimit,
kNestedRecordLimit, kNestedExprLimit, kSwitchCaseLimit, kEnumConstantLimit,
kStringCharLimit, kExternIDLimit, kCaseSensitive, kLimit, kMacroIDLimit,
kMacroParmLimit, kMacroArgLimit, kNestedBlockLimit, kNestedIncludeLimit,
kImplicitDecl, kIoMIDCharLimit, kNestedCondIncluLimit, kBlockIDLimit},

kImplicitDecl, kIoMIDCharLimit, kNestedCondIncluLimit, kBlockIDLimit,
kNestedDeclLimit, kModifyDeclLimit},
"misra/rule_5_1": {kCaseSensitive, kLimit, kImplicitDecl},
"misra/rule_13_2": {kAggressiveMode},
"misra/rule_13_5": {kAggressiveMode},
Expand Down
2 changes: 1 addition & 1 deletion cruleslib/i18n/localize_misra_analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func localizeErrorMessage(result *pb.Result, p *message.Printer) string {
case pb.Result_MISRA_C_2012_RULE_9_5:
return p.Sprintf("[C1201][misra-c2012-9.5]: violation of misra-c2012-9.5")
case pb.Result_MISRA_C_2012_RULE_10_1:
return p.Sprintf("[C0808][misra-c2012-10.1]: violation of misra-c2012-10.1")
return p.Sprintf("[C0808][misra-c2012-10.1]: violation of misra-c2012-10.1\n%s", result.ExternalMessage)
case pb.Result_MISRA_C_2012_RULE_10_2:
return p.Sprintf("[C0807][misra-c2012-10.2]: violation of misra-c2012-10.2")
case pb.Result_MISRA_C_2012_RULE_10_3:
Expand Down
2 changes: 1 addition & 1 deletion cruleslib/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func modifyResult(result *analyzerResult) {
} else if edition == "autosar" {
ruleName = strings.TrimPrefix(ruleName, "rule_")
r.ErrorMessage = fmt.Sprintf("[%s][%s-%s]: %s", ruleName, edition, ruleStr, r.ErrorMessage)
} else {
} else if edition != "misra_c_2012" {
r.ErrorMessage = fmt.Sprintf("[%s][%s-%s]: %s", strings.ToUpper(ruleName), edition, ruleName, r.ErrorMessage)
}
}
Expand Down
15 changes: 14 additions & 1 deletion cruleslib/testlib/testlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,20 @@ import (
"naive.systems/analyzer/misra/utils"
)

var checkingStandards = []string{"gjb5369", "gjb8114", "misra_cpp_2008", "cwe", "autosar", "googlecpp", "clang-tidy", "misra", "cppcheck"}
var checkingStandards = []string{
// BEGIN-INTERNAL
"gjb5369",
"gjb8114",
"cwe",
"clang-tidy",
"cppcheck",
// END-INTERNAL
"misra",
"misra_cpp_2008",
"autosar",
"googlecpp",
"toy_rules",
}

func getCheckerConfig(projectBaseDir string) *proto.CheckerConfiguration {
checkerConfig := proto.CheckerConfiguration{
Expand Down
12 changes: 12 additions & 0 deletions libtooling_includes/cmd_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,18 @@ llvm::cl::opt<int> iom_id_char_limit(
"significant initial characters limit count in an internal identifier or a macro name, default 63"),
llvm::cl::init(63), llvm::cl::cat(ns_libtooling_checker));

llvm::cl::opt<int> nested_decl_limit(
"NestedDeclLimit",
llvm::cl::desc(
"nesting level limit count for parenthesized declaration, default 63"),
llvm::cl::init(63), llvm::cl::cat(ns_libtooling_checker));

llvm::cl::opt<int> modify_decl_limit(
"ModifyDeclLimit",
llvm::cl::desc(
"pointer, array, and function declarators limit count (in any combinations) modifying an arithmetic, structure, union, or void type in a declaration, default 12"),
llvm::cl::init(12), llvm::cl::cat(ns_libtooling_checker));

llvm::cl::opt<int> nested_cond_inclu_limit(
"NestedCondIncluLimit",
llvm::cl::desc(
Expand Down
3 changes: 1 addition & 2 deletions misra/analyzer/cmd/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
LIMIT=0
NUM_WORKERS=0
LICENSED_TO="测试用户"
LANG="zh"
all:
go generate naive.systems/analyzer/misra/...
go build -tags static -o misra_analyzer -ldflags "-X main.linesLimitStr=$(LIMIT) -X main.licensedEntity=$(LICENSED_TO) -X main.numWorkersStr=$(NUM_WORKERS) -X main.lang=$(LANG)"
go build -tags static -o misra_analyzer -ldflags "-X main.linesLimitStr=$(LIMIT) -X main.numWorkersStr=$(NUM_WORKERS) -X main.lang=$(LANG)"
16 changes: 13 additions & 3 deletions misra/analyzer/cmd/locales/en/messages.gotext.json
Original file line number Diff line number Diff line change
Expand Up @@ -1146,10 +1146,20 @@
"fuzzy": true
},
{
"id": "[C0808][misra-c2012-10.1]: violation of misra-c2012-10.1",
"message": "[C0808][misra-c2012-10.1]: violation of misra-c2012-10.1",
"translation": "[C0808][misra-c2012-10.1]: Violation of MISRA C:2012 Rule 10.1: Operands shall not be of an inappropriate essential type",
"id": "[C0808][misra-c2012-10.1]: violation of misra-c2012-10.1\n{ExternalMessage}",
"message": "[C0808][misra-c2012-10.1]: violation of misra-c2012-10.1\n{ExternalMessage}",
"translation": "[C0808][misra-c2012-10.1]: Violation of MISRA C:2012 Rule 10.1: Operands shall not be of an inappropriate essential type\n{ExternalMessage}",
"translatorComment": "Copied from source.",
"placeholders": [
{
"id": "ExternalMessage",
"string": "%[1]s",
"type": "string",
"underlyingType": "string",
"argNum": 1,
"expr": "result.ExternalMessage"
}
],
"fuzzy": true
},
{
Expand Down
16 changes: 13 additions & 3 deletions misra/analyzer/cmd/locales/zh/messages.gotext.json
Original file line number Diff line number Diff line change
Expand Up @@ -1006,9 +1006,19 @@
"translation": "[C1201][misra-c2012-9.5]: 对数组对象进行指定初始化时,必须显式指定数组大小"
},
{
"id": "[C0808][misra-c2012-10.1]: violation of misra-c2012-10.1",
"message": "[C0808][misra-c2012-10.1]: violation of misra-c2012-10.1",
"translation": "[C0808][misra-c2012-10.1]: 操作数不得为不合适的基本类型"
"id": "[C0808][misra-c2012-10.1]: violation of misra-c2012-10.1\n{ExternalMessage}",
"message": "[C0808][misra-c2012-10.1]: violation of misra-c2012-10.1\n{ExternalMessage}",
"translation": "[C0808][misra-c2012-10.1]: 操作数不得为不合适的基本类型\n{ExternalMessage}",
"placeholders": [
{
"id": "ExternalMessage",
"string": "%[1]s",
"type": "string",
"underlyingType": "string",
"argNum": 1,
"expr": "result.ExternalMessage"
}
]
},
{
"id": "[C0807][misra-c2012-10.2]: violation of misra-c2012-10.2",
Expand Down
32 changes: 16 additions & 16 deletions misra/analyzer/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
"naive.systems/analyzer/misra/checker_integration/checkrule"
misra_c_2012_crules "naive.systems/analyzer/misra_c_2012_crules/analyzer"
misra_cpp_2008 "naive.systems/analyzer/misra_cpp_2008/analyzer"
toy_rules "naive.systems/analyzer/toy_rules/analyzer"
)

var compileCommandsPath = "/src/compile_commands.json"
Expand All @@ -53,23 +54,20 @@ var linesLimitStr string = "0"
var numWorkersStr string = "0"
var lang string = "zh"

var ruleSets = []string{
"googlecpp", "misra_cpp_2008", "autosar", "misra_c_2012"}
var ruleSets = map[string]string{
"googlecpp": "cpp",
"misra_cpp_2008": "cpp",
"autosar": "cpp",
"misra_c_2012": "c",
"toy_rules": "cpp",
}

func isCOnly(rule string) bool {
switch rule {
case "misra_c_2012":
return true
}
return false
func isCOnly(ruleSet string) bool {
return ruleSets[ruleSet] == "c"
}

func isCPPOnly(rule string) bool {
switch rule {
case "googlecpp", "misra_cpp_2008", "autosar":
return true
}
return false
func isCPPOnly(ruleSet string) bool {
return ruleSets[ruleSet] == "cpp"
}

func main() {
Expand Down Expand Up @@ -216,15 +214,15 @@ func main() {
csaSystemLibOptionsCopy := parsedCheckerConfig.CsaSystemLibOptions

checkerPathsMap := map[string]string{}
for _, ruleSet := range ruleSets {
for ruleSet := range ruleSets {
if ruleSet == "misra_c_2012" {
checkerPathsMap["misra"] = filepath.Join(sharedOptions.GetCheckerPathRoot(), "misra")
continue
}
checkerPathsMap[ruleSet] = filepath.Join(sharedOptions.GetCheckerPathRoot(), ruleSet)
}

for _, ruleSet := range ruleSets {
for ruleSet := range ruleSets {
if isCOnly(ruleSet) && projType == analyzerinterface.Keil {
parsedCheckerConfig.CsaSystemLibOptions = *options.ConcatStringField("-isystem /usr/include", &parsedCheckerConfig.CsaSystemLibOptions)
} else {
Expand Down Expand Up @@ -387,6 +385,8 @@ func selectRun(rulePrefix string) (runFuncType, error) {
return misra_c_2012_crules.Run, nil
case "misra_cpp_2008":
return misra_cpp_2008.Run, nil
case "toy_rules":
return toy_rules.Run, nil
default:
return nil, fmt.Errorf("No such rule runner found: %s", rulePrefix)
}
Expand Down
6 changes: 6 additions & 0 deletions misra/checker_integration/checkrule/checkrule.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type JSONOption struct {
Limit *int `json:"limit,omitempty" yaml:"-"`
Standard string `json:"standard,omitempty" yaml:"-"`
MaxLoop *int `json:"max-loop,omitempty" yaml:"-"`
MaxNodes *int `json:"max-nodes,omitempty" yaml:"-"`
Filters *FilterAndSinkFuncList `json:"taintFilters,omitempty" yaml:"Filters,omitempty"`
Sources *SourceFuncList `json:"taintSources,omitempty" yaml:"Propagations,omitempty"`
Sinks *FilterAndSinkFuncList `json:"taintSinks,omitempty" yaml:"Sinks,omitempty"`
Expand Down Expand Up @@ -86,6 +87,8 @@ type JSONOption struct {
IoMIDCharLimit *int `json:"iom-id-char-limit,omitempty" yaml:"-"` //misra_c_2012/rule_1_1
NestedCondIncluLimit *int `json:"nested-cond-inclu-limit,omitempty" yaml:"-"` //misra_c_2012/rule_1_1
BlockIDLimit *int `json:"block-id-limit,omitempty" yaml:"-"` //misra_c_2012/rule_1_1
NestedDeclLimit *int `json:"nested-decl-limit,omitempty" yaml:"-"` //misra_c_2012/rule_1_1
ModifyDeclLimit *int `json:"modify-decl-limit,omitempty" yaml:"-"` //misra_c_2012/rule_1_1
}

type FilterAndSinkFunc struct {
Expand Down Expand Up @@ -185,6 +188,9 @@ func (jsonOption *JSONOption) Update(newOption JSONOption) {
if newOption.MaxLoop != nil {
jsonOption.MaxLoop = newOption.MaxLoop
}
if newOption.MaxNodes != nil {
jsonOption.MaxNodes = newOption.MaxNodes
}
if newOption.Sinks != nil {
jsonOption.Sinks = newOption.Sinks
}
Expand Down
4 changes: 4 additions & 0 deletions misra/checker_integration/csa/run_csa.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ func runSingleCSA(
if maxLoop != nil {
cmd_arr = append(cmd_arr, "-Xanalyzer", "-analyzer-max-loop", "-Xanalyzer", strconv.Itoa(*maxLoop))
}
maxNodes := jsonOptions.MaxNodes
if maxNodes != nil {
cmd_arr = append(cmd_arr, "-Xanalyzer", "-analyzer-config", "-Xanalyzer", "max-nodes="+strconv.Itoa(*maxNodes))
}
// this cmd is only for cwe 789
if checkerOptions == "-analyzer-checker=cwe.ExcessiveMemoryAllocation" {
MaximumAllowedSize := jsonOptions.MaximumAllowedSize
Expand Down
24 changes: 24 additions & 0 deletions misra/libtooling_utils/libtooling_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,22 @@ class ASTVisitor : public RecursiveASTVisitor<ASTVisitor> {
funcDecls.emplace_back(fd); // Store the node
return true; // Continue traversal
}
bool VisitArrayType(const ArrayType* at) {
arrayTypes.emplace_back(at); // Store the node
return true; // Continue traversal
}
bool VisitFunctionType(const FunctionType* ft) {
funcTypes.emplace_back(ft); // Store the node
return true; // Continue traversal
}
bool VisitPointerType(const PointerType* pt) {
ptrTypes.emplace_back(pt); // Store the node
return true; // Continue traversal
}
bool VisitParenType(const ParenType* pt) {
parenTypes.emplace_back(pt); // Store the node
return true; // Continue traversal
}
const vector<const CXXMemberCallExpr*>& getMemberCalls() const {
return memberCalls;
}
Expand All @@ -256,6 +272,10 @@ class ASTVisitor : public RecursiveASTVisitor<ASTVisitor> {
return binaryOps;
}
const vector<const FunctionDecl*>& getFuncDecls() const { return funcDecls; }
const vector<const ArrayType*>& getArrayTypes() const { return arrayTypes; }
const vector<const FunctionType*>& getFuncTypes() const { return funcTypes; }
const vector<const PointerType*>& getPtrTypes() const { return ptrTypes; }
const vector<const ParenType*>& getParenTypes() const { return parenTypes; }

private:
vector<const CXXMemberCallExpr*> memberCalls{};
Expand All @@ -266,6 +286,10 @@ class ASTVisitor : public RecursiveASTVisitor<ASTVisitor> {
vector<const VarDecl*> varDecls{};
vector<const BinaryOperator*> binaryOps{};
vector<const FunctionDecl*> funcDecls{};
vector<const ArrayType*> arrayTypes{};
vector<const FunctionType*> funcTypes{};
vector<const PointerType*> ptrTypes{};
vector<const ParenType*> parenTypes{};
};

string GetExprName(const Expr* expr, SourceManager* sm, ASTContext* context);
Expand Down
Loading

0 comments on commit fdc66e5

Please sign in to comment.