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