| Fangrui Song | 524b3c1 | 2019-03-01 06:49:51 +0000 | [diff] [blame] | 1 | //===- ClangExtDefMapGen.cpp -----------------------------------------------===// |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 2 | // |
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 6 | // |
| 7 | //===--------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Clang tool which creates a list of defined functions and the files in which |
| 10 | // they are defined. |
| 11 | // |
| 12 | //===--------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/AST/ASTConsumer.h" |
| 15 | #include "clang/AST/ASTContext.h" |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 16 | #include "clang/Basic/SourceManager.h" |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 17 | #include "clang/CrossTU/CrossTranslationUnit.h" |
| 18 | #include "clang/Frontend/CompilerInstance.h" |
| 19 | #include "clang/Frontend/FrontendActions.h" |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 20 | #include "clang/Tooling/CommonOptionsParser.h" |
| 21 | #include "clang/Tooling/Tooling.h" |
| 22 | #include "llvm/Support/CommandLine.h" |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Signals.h" |
| 24 | #include <sstream> |
| 25 | #include <string> |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace llvm; |
| 28 | using namespace clang; |
| 29 | using namespace clang::cross_tu; |
| 30 | using namespace clang::tooling; |
| 31 | |
| Rafael Stahl | 8c48705 | 2019-01-10 17:44:04 +0000 | [diff] [blame] | 32 | static cl::OptionCategory ClangExtDefMapGenCategory("clang-extdefmapgen options"); |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 33 | |
| Rafael Stahl | 8c48705 | 2019-01-10 17:44:04 +0000 | [diff] [blame] | 34 | class MapExtDefNamesConsumer : public ASTConsumer { |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 35 | public: |
| Rafael Stahl | 8c48705 | 2019-01-10 17:44:04 +0000 | [diff] [blame] | 36 | MapExtDefNamesConsumer(ASTContext &Context) |
| Rafael Stahl | 850361f | 2019-04-23 11:04:41 +0000 | [diff] [blame^] | 37 | : Ctx(Context), SM(Context.getSourceManager()) {} |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 38 | |
| Rafael Stahl | 8c48705 | 2019-01-10 17:44:04 +0000 | [diff] [blame] | 39 | ~MapExtDefNamesConsumer() { |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 40 | // Flush results to standard output. |
| 41 | llvm::outs() << createCrossTUIndexString(Index); |
| 42 | } |
| 43 | |
| Rafael Stahl | 850361f | 2019-04-23 11:04:41 +0000 | [diff] [blame^] | 44 | void HandleTranslationUnit(ASTContext &Context) override { |
| 45 | handleDecl(Context.getTranslationUnitDecl()); |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | private: |
| 49 | void handleDecl(const Decl *D); |
| Rafael Stahl | 850361f | 2019-04-23 11:04:41 +0000 | [diff] [blame^] | 50 | void addIfInMain(const DeclaratorDecl *DD, SourceLocation defStart); |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 51 | |
| Rafael Stahl | 850361f | 2019-04-23 11:04:41 +0000 | [diff] [blame^] | 52 | ASTContext &Ctx; |
| Gabor Horvath | 9c0fdc1 | 2018-11-02 11:22:22 +0000 | [diff] [blame] | 53 | SourceManager &SM; |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 54 | llvm::StringMap<std::string> Index; |
| 55 | std::string CurrentFileName; |
| 56 | }; |
| 57 | |
| Rafael Stahl | 8c48705 | 2019-01-10 17:44:04 +0000 | [diff] [blame] | 58 | void MapExtDefNamesConsumer::handleDecl(const Decl *D) { |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 59 | if (!D) |
| 60 | return; |
| 61 | |
| 62 | if (const auto *FD = dyn_cast<FunctionDecl>(D)) { |
| Rafael Stahl | 850361f | 2019-04-23 11:04:41 +0000 | [diff] [blame^] | 63 | if (FD->isThisDeclarationADefinition()) |
| 64 | if (const Stmt *Body = FD->getBody()) |
| 65 | addIfInMain(FD, Body->getBeginLoc()); |
| 66 | } else if (const auto *VD = dyn_cast<VarDecl>(D)) { |
| 67 | if (cross_tu::containsConst(VD, Ctx) && VD->hasInit()) |
| 68 | if (const Expr *Init = VD->getInit()) |
| 69 | addIfInMain(VD, Init->getBeginLoc()); |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | if (const auto *DC = dyn_cast<DeclContext>(D)) |
| 73 | for (const Decl *D : DC->decls()) |
| 74 | handleDecl(D); |
| 75 | } |
| 76 | |
| Rafael Stahl | 850361f | 2019-04-23 11:04:41 +0000 | [diff] [blame^] | 77 | void MapExtDefNamesConsumer::addIfInMain(const DeclaratorDecl *DD, |
| 78 | SourceLocation defStart) { |
| 79 | std::string LookupName = CrossTranslationUnitContext::getLookupName(DD); |
| 80 | if (CurrentFileName.empty()) { |
| 81 | CurrentFileName = |
| 82 | SM.getFileEntryForID(SM.getMainFileID())->tryGetRealPathName(); |
| 83 | if (CurrentFileName.empty()) |
| 84 | CurrentFileName = "invalid_file"; |
| 85 | } |
| 86 | |
| 87 | switch (DD->getLinkageInternal()) { |
| 88 | case ExternalLinkage: |
| 89 | case VisibleNoLinkage: |
| 90 | case UniqueExternalLinkage: |
| 91 | if (SM.isInMainFile(defStart)) |
| 92 | Index[LookupName] = CurrentFileName; |
| 93 | default: |
| 94 | break; |
| 95 | } |
| 96 | } |
| 97 | |
| Rafael Stahl | 8c48705 | 2019-01-10 17:44:04 +0000 | [diff] [blame] | 98 | class MapExtDefNamesAction : public ASTFrontendAction { |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 99 | protected: |
| 100 | std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, |
| 101 | llvm::StringRef) { |
| Rafael Stahl | 8c48705 | 2019-01-10 17:44:04 +0000 | [diff] [blame] | 102 | return llvm::make_unique<MapExtDefNamesConsumer>(CI.getASTContext()); |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 103 | } |
| 104 | }; |
| 105 | |
| 106 | static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage); |
| 107 | |
| 108 | int main(int argc, const char **argv) { |
| 109 | // Print a stack trace if we signal out. |
| 110 | sys::PrintStackTraceOnErrorSignal(argv[0], false); |
| 111 | PrettyStackTraceProgram X(argc, argv); |
| 112 | |
| 113 | const char *Overview = "\nThis tool collects the USR name and location " |
| Rafael Stahl | 8c48705 | 2019-01-10 17:44:04 +0000 | [diff] [blame] | 114 | "of external definitions in the source files " |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 115 | "(excluding headers).\n"; |
| Rafael Stahl | 8c48705 | 2019-01-10 17:44:04 +0000 | [diff] [blame] | 116 | CommonOptionsParser OptionsParser(argc, argv, ClangExtDefMapGenCategory, |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 117 | cl::ZeroOrMore, Overview); |
| 118 | |
| 119 | ClangTool Tool(OptionsParser.getCompilations(), |
| 120 | OptionsParser.getSourcePathList()); |
| Gabor Horvath | 9c0fdc1 | 2018-11-02 11:22:22 +0000 | [diff] [blame] | 121 | |
| Rafael Stahl | 8c48705 | 2019-01-10 17:44:04 +0000 | [diff] [blame] | 122 | return Tool.run(newFrontendActionFactory<MapExtDefNamesAction>().get()); |
| Gabor Horvath | e350b0a | 2017-09-22 11:11:01 +0000 | [diff] [blame] | 123 | } |