Chromium Code Reviews| 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 // This clang tool finds all instances of net::DefineNetworkTrafficAnnotation in | |
| 6 // given source code, extracts the location info and content of annotation tags | |
| 7 // (unique id and annotation text), and stores them in separate text files | |
| 8 // (per instance) in the given output directory. Please refer to README.md for | |
| 9 // build and usage instructions. | |
| 10 | |
| 11 #include <memory> | |
| 12 #include <vector> | |
| 13 | |
| 14 #include "clang/ASTMatchers/ASTMatchFinder.h" | |
| 15 #include "clang/ASTMatchers/ASTMatchers.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 | |
| 24 using namespace clang::ast_matchers; | |
| 25 | |
| 26 namespace { | |
| 27 | |
| 28 // An instance of a call to the net::DefineNetworkTrafficAnnotation function. | |
| 29 struct NetworkAnnotationInstance { | |
| 30 // Information about where the call has happened. | |
| 31 struct Location { | |
| 32 std::string file_path; | |
| 33 int line_number = -1; | |
| 34 | |
| 35 // Name of the function calling net::DefineNetworkTrafficAnnotation. E.g., | |
| 36 // in the following code, |function_name| will be 'foo': | |
| 37 // void foo() { NetworkTrafficAnnotationTag bar = | |
| 38 // net::DefineNetworkTrafficAnnotation(...); } | |
| 39 // If no function is found, 'Global Namepace' will be returned. | |
|
dcheng
2017/04/12 22:03:56
Nit: missing s in Name_s_pace =)
Ramin Halavati
2017/04/19 12:29:27
Done.
| |
| 40 std::string function_name; | |
| 41 }; | |
| 42 | |
| 43 // Annotation content. These are the parameters of the call to | |
| 44 // net::DefineNetworkTrafficAnnotation. The unique_id is an identifier for the | |
| 45 // annotation that has to be unique across the entire code base. The |text| | |
| 46 // stores a raw string with the annotation that should be extracted. | |
| 47 struct Annotation { | |
| 48 std::string unique_id; | |
| 49 std::string text; | |
| 50 }; | |
| 51 | |
| 52 Location location; | |
| 53 Annotation annotation; | |
| 54 }; | |
| 55 | |
| 56 using Collector = std::vector<NetworkAnnotationInstance>; | |
| 57 | |
| 58 // This class implements the call back functions for AST Matchers. The matchers | |
| 59 // are defined in RunMatchers function. When a pattern is found there, | |
| 60 // the run function in this class is called back with information on the matched | |
| 61 // location and description of the matched pattern. | |
| 62 class NetworkAnnotationTagCallback : public MatchFinder::MatchCallback { | |
| 63 public: | |
| 64 explicit NetworkAnnotationTagCallback(Collector* collector) | |
| 65 : collector_(collector) {} | |
| 66 ~NetworkAnnotationTagCallback() override = default; | |
| 67 | |
| 68 // Is called on any pattern found by ASTMathers that are defined in RunMathers | |
| 69 // function. | |
| 70 virtual void run(const MatchFinder::MatchResult& result) override { | |
| 71 const clang::CallExpr* call_expr = | |
| 72 result.Nodes.getNodeAs<clang::CallExpr>("definition_function"); | |
| 73 const clang::StringLiteral* unique_id = | |
| 74 result.Nodes.getNodeAs<clang::StringLiteral>("unique_id"); | |
| 75 const clang::StringLiteral* annotation_text = | |
| 76 result.Nodes.getNodeAs<clang::StringLiteral>("annotation_text"); | |
| 77 const clang::FunctionDecl* ancestor = | |
| 78 result.Nodes.getNodeAs<clang::FunctionDecl>("function_context"); | |
| 79 | |
| 80 assert(call_expr && unique_id && annotation_text); | |
| 81 | |
| 82 NetworkAnnotationInstance instance; | |
| 83 instance.annotation.unique_id = unique_id->getString(); | |
| 84 instance.annotation.text = annotation_text->getString(); | |
| 85 | |
| 86 // Get annotation location. | |
| 87 clang::SourceLocation source_location = call_expr->getLocStart(); | |
| 88 if (source_location.isMacroID()) { | |
| 89 source_location = | |
| 90 result.SourceManager->getImmediateMacroCallerLoc(source_location); | |
| 91 } | |
| 92 instance.location.file_path = | |
| 93 result.SourceManager->getFilename(source_location); | |
| 94 instance.location.line_number = | |
| 95 result.SourceManager->getSpellingLineNumber(source_location); | |
| 96 if (ancestor) | |
| 97 instance.location.function_name = ancestor->getQualifiedNameAsString(); | |
| 98 else | |
| 99 instance.location.function_name = "Global Namespace"; | |
| 100 | |
| 101 // Trim leading "../"s from file path. | |
| 102 std::replace(instance.location.file_path.begin(), | |
| 103 instance.location.file_path.end(), '\\', '/'); | |
| 104 while (instance.location.file_path.length() > 3 && | |
| 105 instance.location.file_path.substr(0, 3) == "../") { | |
| 106 instance.location.file_path = instance.location.file_path.substr( | |
| 107 3, instance.location.file_path.length() - 3); | |
| 108 } | |
| 109 | |
| 110 collector_->push_back(instance); | |
| 111 } | |
| 112 | |
| 113 private: | |
| 114 Collector* collector_; | |
| 115 }; | |
| 116 | |
| 117 // Sets up an ASTMatcher and runs clang tool to populate collector. Returns the | |
| 118 // result of running the clang tool. | |
| 119 int RunMatchers(clang::tooling::ClangTool* clang_tool, Collector* collector) { | |
| 120 NetworkAnnotationTagCallback callback(collector); | |
| 121 MatchFinder match_finder; | |
| 122 | |
| 123 // Set up a pattern to find functions that are named | |
| 124 // [net::]DefineNetworkTrafficAnnotation and have 2 arguments of string | |
| 125 // literal type. If pattern has a function declaration as ancestor, it is | |
| 126 // marked. | |
| 127 match_finder.addMatcher( | |
| 128 callExpr(hasDeclaration(functionDecl( | |
| 129 anyOf(hasName("DefineNetworkTrafficAnnotation"), | |
| 130 hasName("net::DefineNetworkTrafficAnnotation")))), | |
| 131 hasArgument(0, stringLiteral().bind("unique_id")), | |
| 132 hasArgument(1, stringLiteral().bind("annotation_text")), | |
| 133 anyOf(hasAncestor(functionDecl().bind("function_context")), | |
| 134 unless(hasAncestor(functionDecl())))) | |
| 135 .bind("definition_function"), | |
| 136 &callback); | |
| 137 std::unique_ptr<clang::tooling::FrontendActionFactory> frontend_factory = | |
| 138 clang::tooling::newFrontendActionFactory(&match_finder); | |
| 139 return clang_tool->run(frontend_factory.get()); | |
| 140 } | |
| 141 | |
| 142 } // namespace | |
| 143 | |
| 144 static llvm::cl::OptionCategory ToolCategory( | |
| 145 "traffic_annotation_extractor: Extract traffic annotation texts"); | |
| 146 static llvm::cl::extrahelp CommonHelp( | |
| 147 clang::tooling::CommonOptionsParser::HelpMessage); | |
| 148 | |
| 149 int main(int argc, const char* argv[]) { | |
| 150 clang::tooling::CommonOptionsParser options(argc, argv, ToolCategory); | |
| 151 clang::tooling::ClangTool tool(options.getCompilations(), | |
| 152 options.getSourcePathList()); | |
| 153 Collector collector; | |
| 154 | |
| 155 int result = RunMatchers(&tool, &collector); | |
| 156 | |
| 157 if (result != 0) | |
| 158 return result; | |
| 159 | |
| 160 // For each call to "DefineNetworkTrafficAnnotation", write annotation text | |
| 161 // and relevant meta data into a separate file. The filename is uniquely | |
| 162 // generated using the file path and filename of the code including the call | |
| 163 // and its line number. | |
| 164 for (NetworkAnnotationInstance& call : collector) { | |
|
dcheng
2017/04/12 22:03:56
nit: const as well
(it's also acceptable to just
Ramin Halavati
2017/04/19 12:29:27
Done.
| |
| 165 llvm::outs() << "==== NEW ANNOTATION ====\n"; | |
| 166 llvm::outs() << call.location.file_path << "\n"; | |
| 167 llvm::outs() << call.location.function_name << "\n"; | |
| 168 llvm::outs() << call.location.line_number << "\n"; | |
| 169 llvm::outs() << call.annotation.unique_id << "\n"; | |
| 170 llvm::outs() << call.annotation.text << "\n"; | |
| 171 llvm::outs() << "==== ANNOTATION ENDS ====\n"; | |
| 172 } | |
| 173 | |
| 174 return 0; | |
| 175 } | |
| OLD | NEW |