Skip to content

Commit 346587a

Browse files
committed
add more regex to handle empty qutoes and extra spaces
1 parent cc301a5 commit 346587a

1 file changed

Lines changed: 28 additions & 21 deletions

File tree

cli/cppcheckexecutor.cpp

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,19 @@ namespace {
8989
static std::string getRuleDescription(const std::string& ruleId, bool fullDescription) {
9090
static std::unordered_map<std::string, std::pair<std::string, std::string>> ruleCache;
9191
static bool cacheInitialized = false;
92-
92+
9393
if (!cacheInitialized) {
9494
initializeRuleCache(ruleCache);
9595
cacheInitialized = true;
9696
}
97-
97+
9898
const auto it = ruleCache.find(ruleId);
9999
if (it != ruleCache.end()) {
100100
const std::string& description = fullDescription ? it->second.second : it->second.first;
101101
// Convert instance-specific descriptions to generic ones
102102
return makeGeneric(description, ruleId);
103103
}
104-
104+
105105
// Fallback for rules not found in cache
106106
return "Cppcheck rule " + ruleId;
107107
}
@@ -110,44 +110,51 @@ namespace {
110110
// Convert instance-specific descriptions to generic ones
111111
static std::string makeGeneric(const std::string& description, const std::string& ruleId) {
112112
std::string result = description;
113-
113+
114114
// Common patterns to genericize
115115
// Array access patterns
116116
if (ruleId == "arrayIndexOutOfBounds" || ruleId == "arrayIndexOutOfBoundsCond") {
117117
// Replace "Array 'arr[16]' accessed at index 16" with "Array accessed at index that is out of bounds"
118118
std::regex arrayPattern(R"(Array '[^']*' accessed at index \d+, which is out of bounds\.)");
119119
result = std::regex_replace(result, arrayPattern, "Array accessed at index that is out of bounds.");
120120
}
121-
122-
// Null pointer patterns
123-
if (ruleId.find("nullPointer") != std::string::npos) {
124-
// Keep simple messages as they are already generic
125-
// "Null pointer dereference" is already generic
126-
return result;
127-
}
128-
129-
// Division by zero
130-
if (ruleId == "zerodiv" || ruleId == "zerodivcond") {
131-
// "Division by zero." is already generic
132-
return result;
133-
}
134-
121+
135122
// Variable name patterns - replace specific variable names with generic terms
136123
result = std::regex_replace(result, std::regex(R"('arr\[\d+\]')"), "'array'");
137124
result = std::regex_replace(result, std::regex(R"('varname')"), "'variable'");
138125
result = std::regex_replace(result, std::regex(R"('[a-zA-Z_][a-zA-Z0-9_]*')"), "'variable'");
139-
126+
140127
// Number patterns - replace specific numbers with generic terms
141128
result = std::regex_replace(result, std::regex(R"( \d+ )"), " N ");
142129
result = std::regex_replace(result, std::regex(R"( at index \d+)"), " at index N");
143-
130+
144131
// Function name patterns
145132
result = std::regex_replace(result, std::regex(R"(function '[^']*')"), "function");
146133
result = std::regex_replace(result, std::regex(R"(Function '[^']*')"), "Function");
134+
135+
// Clean up empty quotes and redundant spaces
136+
// Handle cases where parameter/variable names were empty or completely removed
137+
result = std::regex_replace(result, std::regex(R"(parameter ''\s)"), "parameter ");
138+
result = std::regex_replace(result, std::regex(R"(Parameter ''\s)"), "Parameter ");
139+
result = std::regex_replace(result, std::regex(R"(variable ''\s)"), "variable ");
140+
result = std::regex_replace(result, std::regex(R"(Variable ''\s)"), "Variable ");
141+
result = std::regex_replace(result, std::regex(R"(function ''\s)"), "function ");
142+
result = std::regex_replace(result, std::regex(R"(Function ''\s)"), "Function ");
143+
144+
// Handle standalone empty quotes
145+
result = std::regex_replace(result, std::regex(R"(\s''\s)"), " ");
146+
result = std::regex_replace(result, std::regex(R"(^''\s)"), "");
147+
result = std::regex_replace(result, std::regex(R"(\s''$)"), "");
148+
149+
// Clean up multiple spaces
150+
result = std::regex_replace(result, std::regex(R"(\s+)"), " ");
147151

152+
// Trim leading and trailing whitespace
153+
result = std::regex_replace(result, std::regex(R"(^\s+|\s+$)"), "");
154+
148155
return result;
149156
}
150-
157+
151158
private:
152159
// Error logger that captures error messages for building the rule cache
153160
class RuleCacheLogger : public ErrorLogger {

0 commit comments

Comments
 (0)