summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/clang-tidy/misc/InefficientAlgorithmCheck.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'clang-tools-extra/clang-tidy/misc/InefficientAlgorithmCheck.cpp')
-rw-r--r--clang-tools-extra/clang-tidy/misc/InefficientAlgorithmCheck.cpp45
1 files changed, 34 insertions, 11 deletions
diff --git a/clang-tools-extra/clang-tidy/misc/InefficientAlgorithmCheck.cpp b/clang-tools-extra/clang-tidy/misc/InefficientAlgorithmCheck.cpp
index 8d58a814951..3ca6c8196d7 100644
--- a/clang-tools-extra/clang-tidy/misc/InefficientAlgorithmCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/InefficientAlgorithmCheck.cpp
@@ -105,18 +105,41 @@ void InefficientAlgorithmCheck::check(const MatchFinder::MatchResult &Result) {
const auto *IneffContExpr = Result.Nodes.getNodeAs<Expr>("IneffContExpr");
FixItHint Hint;
- if (!AlgCall->getLocStart().isMacroID() && !Maplike && CompatibleTypes) {
+ SourceManager &SM = *Result.SourceManager;
+ LangOptions LangOpts = Result.Context->getLangOpts();
+
+ CharSourceRange CallRange =
+ CharSourceRange::getTokenRange(AlgCall->getSourceRange());
+
+ // FIXME: Create a common utility to extract a file range that the given token
+ // sequence is exactly spelled at (without macro argument expansions etc.).
+ // We can't use Lexer::makeFileCharRange here, because for
+ //
+ // #define F(x) x
+ // x(a b c);
+ //
+ // it will return "x(a b c)", when given the range "a"-"c". It makes sense for
+ // removals, but not for replacements.
+ //
+ // This code is over-simplified, but works for many real cases.
+ if (SM.isMacroArgExpansion(CallRange.getBegin()) &&
+ SM.isMacroArgExpansion(CallRange.getEnd())) {
+ CallRange.setBegin(SM.getSpellingLoc(CallRange.getBegin()));
+ CallRange.setEnd(SM.getSpellingLoc(CallRange.getEnd()));
+ }
+
+ if (!CallRange.getBegin().isMacroID() && !Maplike && CompatibleTypes) {
+ StringRef ContainerText = Lexer::getSourceText(
+ CharSourceRange::getTokenRange(IneffContExpr->getSourceRange()), SM,
+ LangOpts);
+ StringRef ParamText = Lexer::getSourceText(
+ CharSourceRange::getTokenRange(AlgParam->getSourceRange()), SM,
+ LangOpts);
std::string ReplacementText =
- (llvm::Twine(Lexer::getSourceText(
- CharSourceRange::getTokenRange(IneffContExpr->getSourceRange()),
- *Result.SourceManager, Result.Context->getLangOpts())) +
- (PtrToContainer ? "->" : ".") + AlgDecl->getName() + "(" +
- Lexer::getSourceText(
- CharSourceRange::getTokenRange(AlgParam->getSourceRange()),
- *Result.SourceManager, Result.Context->getLangOpts()) +
- ")").str();
- Hint = FixItHint::CreateReplacement(AlgCall->getSourceRange(),
- ReplacementText);
+ (llvm::Twine(ContainerText) + (PtrToContainer ? "->" : ".") +
+ AlgDecl->getName() + "(" + ParamText + ")")
+ .str();
+ Hint = FixItHint::CreateReplacement(CallRange, ReplacementText);
}
diag(AlgCall->getLocStart(),
OpenPOWER on IntegriCloud