================
@@ -112,43 +122,91 @@ void ConstCorrectnessCheck::registerMatchers(MatchFinder
*Finder) {
const auto FunctionPointerRef =
hasType(hasCanonicalType(referenceType(pointee(functionType()))));
+ const auto CommonExcludeTypes =
+ anyOf(ConstType, ConstReference, RValueReference, TemplateType,
+ FunctionPointerRef, hasType(cxxRecordDecl(isLambda())),
+ AutoTemplateType, isImplicit(), AllowedType);
+
// Match local variables which could be 'const' if not modified later.
// Example: `int i = 10` would match `int i`.
- const auto LocalValDecl = varDecl(
- isLocal(), hasInitializer(anything()),
- unless(anyOf(ConstType, ConstReference, TemplateType,
- hasInitializer(isInstantiationDependent()),
AutoTemplateType,
- RValueReference, FunctionPointerRef,
- hasType(cxxRecordDecl(isLambda())), isImplicit(),
- AllowedType)));
+ const auto LocalValDecl =
+ varDecl(isLocal(), hasInitializer(unless(isInstantiationDependent())),
+ unless(CommonExcludeTypes));
// Match the function scope for which the analysis of all local variables
// shall be run.
const auto FunctionScope =
- functionDecl(
- hasBody(stmt(forEachDescendant(
- declStmt(containsAnyDeclaration(
- LocalValDecl.bind("local-value")),
- unless(has(decompositionDecl())))
- .bind("decl-stmt")))
- .bind("scope")))
+ functionDecl(hasBody(stmt(forEachDescendant(
+ declStmt(containsAnyDeclaration(
+ LocalValDecl.bind("value")),
+ unless(has(decompositionDecl())))
+ .bind("decl-stmt")))
+ .bind("scope")))
.bind("function-decl");
Finder->addMatcher(FunctionScope, this);
+
+ if (AnalyzeParameters) {
+ const auto ParamMatcher =
+ parmVarDecl(unless(CommonExcludeTypes),
+ anyOf(hasType(referenceType()), hasType(pointerType())))
+ .bind("value");
+
+ // Match function parameters which could be 'const' if not modified later.
+ // Example: `void foo(int* ptr)` would match `int* ptr`.
+ const auto FunctionWithParams =
+ functionDecl(
+ hasBody(stmt().bind("scope")), has(typeLoc(forEach(ParamMatcher))),
+ unless(cxxMethodDecl()),
unless(isFunctionTemplateSpecialization()),
+ unless(isTemplate()))
+ .bind("function-decl");
+
+ Finder->addMatcher(FunctionWithParams, this);
+ }
+}
+
+static void addConstFixits(DiagnosticBuilder &Diag, const VarDecl *Variable,
+ const FunctionDecl *Function, ASTContext &Context,
+ Qualifiers::TQ Qualifier,
+ utils::fixit::QualifierTarget Target,
+ utils::fixit::QualifierPolicy Policy) {
+ Diag << addQualifierToVarDecl(*Variable, Context, Qualifier, Target, Policy);
+
+ // If this is a parameter, also add fixits for corresponding parameters in
+ // function declarations
+ if (const auto *ParamDecl = dyn_cast<ParmVarDecl>(Variable)) {
+ const unsigned ParamIdx = ParamDecl->getFunctionScopeIndex();
+
+ for (const FunctionDecl *Redecl : Function->redecls()) {
+ if (Redecl == Function)
+ continue;
----------------
localspook wrote:
This condition is here because we already provide a fix-it for `Function` on
line 173, is that right? Could we avoid special casing it like so?
```diff
-Diag << addQualifierToVarDecl(*Variable, Context, Qualifier, Target, Policy);
if (const auto *ParamDecl = dyn_cast<ParmVarDecl>(Variable)) {
...
+} else {
+ Diag << addQualifierToVarDecl(*Variable, Context, Qualifier, Target, Policy);
+}
```
https://github.com/llvm/llvm-project/pull/171215
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits