================
@@ -0,0 +1,90 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "LoopVariableCopiedThenModifiedCheck.h"
+#include "../utils/Matchers.h"
+#include "../utils/TypeTraits.h"
+#include "clang/ASTMatchers/ASTMatchFinder.h"
+#include "clang/Analysis/Analyses/ExprMutationAnalyzer.h"
+#include "clang/Basic/Diagnostic.h"
+
+using namespace clang::ast_matchers;
+
+namespace clang::tidy::bugprone {
+
+LoopVariableCopiedThenModifiedCheck::LoopVariableCopiedThenModifiedCheck(
+ StringRef Name, ClangTidyContext *Context)
+ : ClangTidyCheck(Name, Context),
+ IgnoreInexpensiveVariables(
+ Options.get("IgnoreInexpensiveVariables", false)) {}
+
+void LoopVariableCopiedThenModifiedCheck::storeOptions(
+ ClangTidyOptions::OptionMap &Opts) {
+ Options.store(Opts, "IgnoreInexpensiveVariables",
IgnoreInexpensiveVariables);
+}
+
+void LoopVariableCopiedThenModifiedCheck::registerMatchers(
+ MatchFinder *Finder) {
+ auto HasReferenceOrPointerTypeOrIsAllowed = hasType(qualType(
+ unless(hasCanonicalType(anyOf(referenceType(), pointerType())))));
+ auto IteratorReturnsValueType = cxxOperatorCallExpr(
+ hasOverloadedOperatorName("*"),
+ callee(
+ cxxMethodDecl(returns(unless(hasCanonicalType(referenceType()))))));
+ auto NotConstructedByCopy = cxxConstructExpr(
+ hasDeclaration(cxxConstructorDecl(unless(isCopyConstructor()))));
+ auto ConstructedByConversion =
cxxMemberCallExpr(callee(cxxConversionDecl()));
+ auto LoopVar =
+ varDecl(HasReferenceOrPointerTypeOrIsAllowed,
+ unless(hasInitializer(expr(hasDescendant(expr(
+ anyOf(materializeTemporaryExpr(), IteratorReturnsValueType,
+ NotConstructedByCopy, ConstructedByConversion)))))));
+ Finder->addMatcher(cxxForRangeStmt(hasLoopVariable(LoopVar.bind("loopVar")))
+ .bind("forRange"),
+ this);
+}
+
+void LoopVariableCopiedThenModifiedCheck::check(
+ const MatchFinder::MatchResult &Result) {
+ const auto *LoopVar = Result.Nodes.getNodeAs<VarDecl>("loopVar");
+ if (LoopVar->getBeginLoc().isMacroID())
+ return;
+ std::optional<bool> Expensive = utils::type_traits::isExpensiveToCopy(
+ LoopVar->getType(), *Result.Context);
+ if ((!Expensive || !*Expensive) && IgnoreInexpensiveVariables)
+ return;
+ const auto *ForRange = Result.Nodes.getNodeAs<CXXForRangeStmt>("forRange");
+
+ std::string hintString = "";
+
+ // if (ExprMutationAnalyzer(*ForRange->getBody(),
+ // this->getContext()).isMutated(LoopVar)) {
+ if (ExprMutationAnalyzer(*ForRange->getBody(), *Result.Context)
+ .isMutated(LoopVar)) {
+ if (isa<AutoType>(LoopVar->getType())) {
+ hintString = "const auto&";
+ } else {
+ const std::string CanonicalTypeStr =
+ LoopVar->getType().getAsString(Result.Context->getLangOpts());
+ hintString = "const " + CanonicalTypeStr + "&";
+ }
+ clang::SourceRange loopvar_source_range =
----------------
nicovank wrote:
```suggestion
clang::SourceRange LoopVarSourceRange =
```
https://github.com/llvm/llvm-project/pull/157213
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits