Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(197)

Side by Side Diff: tools/clang/move_raw/MoveRaw.cpp

Issue 2919243002: Add a clang tool to detect std::move(raw ptr) (Closed)
Patch Set: Add a TODO to make this a clang-tidy check Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tools/clang/move_raw/CMakeLists.txt ('k') | tools/clang/move_raw/tests/test-expected.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 // TODO(crbug.com/731577): Make this a clang-tidy check instead.
9
10 #include <memory>
11 #include <set>
12 #include <string>
13
14 #include "clang/AST/ASTContext.h"
15 #include "clang/ASTMatchers/ASTMatchFinder.h"
16 #include "clang/ASTMatchers/ASTMatchers.h"
17 #include "clang/ASTMatchers/ASTMatchersMacros.h"
18 #include "clang/Basic/SourceManager.h"
19 #include "clang/Frontend/FrontendActions.h"
20 #include "clang/Lex/Lexer.h"
21 #include "clang/Tooling/CommonOptionsParser.h"
22 #include "clang/Tooling/Refactoring.h"
23 #include "clang/Tooling/Tooling.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/TargetSelect.h"
26
27 using namespace clang::ast_matchers;
28 using clang::tooling::CommonOptionsParser;
29
30 namespace {
31
32 class MoveCallCollector : public MatchFinder::MatchCallback {
33 public:
34 explicit MoveCallCollector(
35 std::set<clang::tooling::Replacement>* replacements)
36 : replacements_(replacements) {}
37 virtual void run(const MatchFinder::MatchResult& result) override;
38
39 private:
40 std::set<clang::tooling::Replacement>* const replacements_;
41 };
42
43 void MoveCallCollector::run(const MatchFinder::MatchResult& result) {
44 const clang::Expr* callsite =
45 result.Nodes.getNodeAs<clang::Expr>("move_call");
46 replacements_->insert(clang::tooling::Replacement(
47 *result.SourceManager,
48 result.SourceManager->getSpellingLoc(callsite->getLocStart()), 0,
49 "/*This tries to move a raw pointer!*/"));
50 }
51
52 } // namespace
53
54 static llvm::cl::extrahelp common_help(CommonOptionsParser::HelpMessage);
55
56 int main(int argc, const char* argv[]) {
57 // TODO(dcheng): Clang tooling should do this itself.
58 // https://llvm.org/bugs/show_bug.cgi?id=21627
59 llvm::InitializeNativeTarget();
60 llvm::InitializeNativeTargetAsmParser();
61 llvm::cl::OptionCategory category(
62 "Catching red flags: calling std::move on raw pointers");
63 CommonOptionsParser options(argc, argv, category);
64 clang::tooling::ClangTool tool(options.getCompilations(),
65 options.getSourcePathList());
66
67 MatchFinder match_finder;
68 std::set<clang::tooling::Replacement> replacements;
69
70 StatementMatcher move_on_raw_matcher =
71 callExpr(argumentCountIs(1), callee(functionDecl(hasName("::std::move"))),
72 hasArgument(0, hasType(pointerType())))
73 .bind("move_call");
74 MoveCallCollector callback(&replacements);
75 match_finder.addMatcher(move_on_raw_matcher, &callback);
76
77 std::unique_ptr<clang::tooling::FrontendActionFactory> factory =
78 clang::tooling::newFrontendActionFactory(&match_finder);
79 int result = tool.run(factory.get());
80 if (result != 0)
81 return result;
82
83 if (replacements.empty())
84 return 0;
85
86 // Serialization format is documented in tools/clang/scripts/run_tool.py
87 llvm::outs() << "==== BEGIN EDITS ====\n";
88 for (const auto& r : replacements) {
89 std::string replacement_text = r.getReplacementText().str();
90 std::replace(replacement_text.begin(), replacement_text.end(), '\n', '\0');
91 llvm::outs() << "r:::" << r.getFilePath() << ":::" << r.getOffset()
92 << ":::" << r.getLength() << ":::" << replacement_text << "\n";
93 }
94 llvm::outs() << "==== END EDITS ====\n";
95
96 return 0;
97 }
OLDNEW
« no previous file with comments | « tools/clang/move_raw/CMakeLists.txt ('k') | tools/clang/move_raw/tests/test-expected.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698