OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 // | |
5 // Clang tool to find where std::move is called on a raw pointer. | |
6 // Calling std::move on a raw pointer has no useful effect and is likely a | |
7 // sign of an error (e.g., mistaking a raw pointer for a smart pointer). | |
8 | |
9 #include <memory> | |
10 #include <string> | |
11 | |
12 #include "clang/AST/ASTContext.h" | |
13 #include "clang/ASTMatchers/ASTMatchFinder.h" | |
14 #include "clang/ASTMatchers/ASTMatchers.h" | |
15 #include "clang/ASTMatchers/ASTMatchersMacros.h" | |
16 #include "clang/Basic/SourceManager.h" | |
17 #include "clang/Frontend/FrontendActions.h" | |
18 #include "clang/Lex/Lexer.h" | |
19 #include "clang/Tooling/CommonOptionsParser.h" | |
20 #include "clang/Tooling/Refactoring.h" | |
21 #include "clang/Tooling/Tooling.h" | |
22 #include "llvm/Support/CommandLine.h" | |
23 #include "llvm/Support/TargetSelect.h" | |
24 | |
25 using namespace clang::ast_matchers; | |
26 using clang::tooling::CommonOptionsParser; | |
27 using clang::tooling::Replacements; | |
28 | |
29 namespace { | |
30 | |
31 class MoveCallCollector : public MatchFinder::MatchCallback { | |
32 public: | |
33 explicit MoveCallCollector(Replacements* replacements) | |
34 : replacements_(replacements) {} | |
35 virtual void run(const MatchFinder::MatchResult& result) override; | |
36 | |
37 private: | |
38 Replacements* const replacements_; | |
39 }; | |
40 | |
41 void MoveCallCollector::run(const MatchFinder::MatchResult& result) { | |
42 const clang::Expr* callsite = | |
43 result.Nodes.getNodeAs<clang::Expr>("move_call"); | |
44 auto err = replacements_->add(clang::tooling::Replacement( | |
vabr (Chromium)
2017/06/04 13:22:24
It turned out that this hit errors in ~120 files w
dcheng
2017/06/07 19:58:34
I would suggest just using a std::set<clang::tooli
vabr (Chromium)
2017/06/08 12:37:49
Done, with vector, because I'm not sure I need the
dcheng
2017/06/08 19:40:42
Well, why emit duplicates? =)
vabr (Chromium)
2017/06/09 07:02:57
By "not sure I need the uniqueness" I meant "I can
dcheng
2017/06/09 07:08:32
I suppose in the context of how these tools are ty
vabr (Chromium)
2017/06/09 07:19:38
Got it, thanks! I agree that set is a better choic
| |
45 *result.SourceManager, | |
46 result.SourceManager->getSpellingLoc(callsite->getLocStart()), 0, | |
47 "/*This tries to move a raw pointer!*/")); | |
48 assert(!err); | |
49 } | |
50 | |
51 } // namespace | |
52 | |
53 static llvm::cl::extrahelp common_help(CommonOptionsParser::HelpMessage); | |
54 | |
55 int main(int argc, const char* argv[]) { | |
56 // TODO(dcheng): Clang tooling should do this itself. | |
57 // https://llvm.org/bugs/show_bug.cgi?id=21627 | |
58 llvm::InitializeNativeTarget(); | |
59 llvm::InitializeNativeTargetAsmParser(); | |
60 llvm::cl::OptionCategory category( | |
61 "Catching red flags: calling std::move on raw pointers"); | |
62 CommonOptionsParser options(argc, argv, category); | |
63 clang::tooling::ClangTool tool(options.getCompilations(), | |
64 options.getSourcePathList()); | |
65 | |
66 MatchFinder match_finder; | |
67 Replacements replacements; | |
68 | |
69 StatementMatcher move_on_raw_matcher = | |
70 callExpr(argumentCountIs(1), callee(functionDecl(hasName("::std::move"))), | |
71 hasArgument(0, hasType(pointerType()))) | |
72 .bind("move_call"); | |
73 MoveCallCollector callback(&replacements); | |
74 match_finder.addMatcher(move_on_raw_matcher, &callback); | |
75 | |
76 std::unique_ptr<clang::tooling::FrontendActionFactory> factory = | |
77 clang::tooling::newFrontendActionFactory(&match_finder); | |
78 int result = tool.run(factory.get()); | |
79 if (result != 0) | |
80 return result; | |
81 | |
82 // Serialization format is documented in tools/clang/scripts/run_tool.py | |
83 llvm::outs() << "==== BEGIN EDITS ====\n"; | |
vabr (Chromium)
2017/06/04 10:56:17
I was wondering if I should only emit BEGIN EDITS
dcheng
2017/06/07 19:58:34
I don't think it matters too much, since we need t
vabr (Chromium)
2017/06/08 12:37:49
If it does not matter much, then I propose to make
dcheng
2017/06/08 19:40:42
*shrug*
I don't feel strongly. But 2 extra lines
vabr (Chromium)
2017/06/09 07:02:57
It was not 2 extra lines. When I ran the tool, ten
dcheng
2017/06/09 07:06:41
Sorry, to clarify, I mean 2 extra lines per invoca
vabr (Chromium)
2017/06/09 07:19:38
My apologies, I was sloppy with the estimate (I di
| |
84 for (const auto& r : replacements) { | |
85 std::string replacement_text = r.getReplacementText().str(); | |
86 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0'); | |
87 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset() | |
88 << ":::" << r.getLength() << ":::" << replacement_text << "\n"; | |
89 } | |
90 llvm::outs() << "==== END EDITS ====\n"; | |
91 | |
92 return 0; | |
93 } | |
OLD | NEW |