2019-03-01 14:49:51 +08:00
|
|
|
//===- ClangExtDefMapGen.cpp -----------------------------------------------===//
|
2017-09-22 19:11:01 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-09-22 19:11:01 +08:00
|
|
|
//
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Clang tool which creates a list of defined functions and the files in which
|
|
|
|
// they are defined.
|
|
|
|
//
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/AST/ASTConsumer.h"
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/CrossTU/CrossTranslationUnit.h"
|
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Frontend/FrontendActions.h"
|
|
|
|
#include "clang/Tooling/CommonOptionsParser.h"
|
|
|
|
#include "clang/Tooling/Tooling.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Signals.h"
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace clang;
|
|
|
|
using namespace clang::cross_tu;
|
|
|
|
using namespace clang::tooling;
|
|
|
|
|
[analyzer][CrossTU][NFC] Generalize to external definitions instead of external functions
Summary: This is just changing naming and documentation to be general about external definitions that can be imported for cross translation unit analysis. There is at least a plan to add VarDecls: D46421
Reviewers: NoQ, xazax.hun, martong, a.sidorin, george.karpenkov, serge-sans-paille
Reviewed By: xazax.hun, martong
Subscribers: mgorny, whisperity, baloghadamsoftware, szepet, rnkovacs, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, cfe-commits
Differential Revision: https://reviews.llvm.org/D56441
llvm-svn: 350852
2019-01-11 01:44:04 +08:00
|
|
|
static cl::OptionCategory ClangExtDefMapGenCategory("clang-extdefmapgen options");
|
2017-09-22 19:11:01 +08:00
|
|
|
|
[analyzer][CrossTU][NFC] Generalize to external definitions instead of external functions
Summary: This is just changing naming and documentation to be general about external definitions that can be imported for cross translation unit analysis. There is at least a plan to add VarDecls: D46421
Reviewers: NoQ, xazax.hun, martong, a.sidorin, george.karpenkov, serge-sans-paille
Reviewed By: xazax.hun, martong
Subscribers: mgorny, whisperity, baloghadamsoftware, szepet, rnkovacs, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, cfe-commits
Differential Revision: https://reviews.llvm.org/D56441
llvm-svn: 350852
2019-01-11 01:44:04 +08:00
|
|
|
class MapExtDefNamesConsumer : public ASTConsumer {
|
2017-09-22 19:11:01 +08:00
|
|
|
public:
|
[analyzer][CrossTU][NFC] Generalize to external definitions instead of external functions
Summary: This is just changing naming and documentation to be general about external definitions that can be imported for cross translation unit analysis. There is at least a plan to add VarDecls: D46421
Reviewers: NoQ, xazax.hun, martong, a.sidorin, george.karpenkov, serge-sans-paille
Reviewed By: xazax.hun, martong
Subscribers: mgorny, whisperity, baloghadamsoftware, szepet, rnkovacs, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, cfe-commits
Differential Revision: https://reviews.llvm.org/D56441
llvm-svn: 350852
2019-01-11 01:44:04 +08:00
|
|
|
MapExtDefNamesConsumer(ASTContext &Context)
|
2018-11-02 19:22:22 +08:00
|
|
|
: SM(Context.getSourceManager()) {}
|
2017-09-22 19:11:01 +08:00
|
|
|
|
[analyzer][CrossTU][NFC] Generalize to external definitions instead of external functions
Summary: This is just changing naming and documentation to be general about external definitions that can be imported for cross translation unit analysis. There is at least a plan to add VarDecls: D46421
Reviewers: NoQ, xazax.hun, martong, a.sidorin, george.karpenkov, serge-sans-paille
Reviewed By: xazax.hun, martong
Subscribers: mgorny, whisperity, baloghadamsoftware, szepet, rnkovacs, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, cfe-commits
Differential Revision: https://reviews.llvm.org/D56441
llvm-svn: 350852
2019-01-11 01:44:04 +08:00
|
|
|
~MapExtDefNamesConsumer() {
|
2017-09-22 19:11:01 +08:00
|
|
|
// Flush results to standard output.
|
|
|
|
llvm::outs() << createCrossTUIndexString(Index);
|
|
|
|
}
|
|
|
|
|
2018-11-02 19:22:22 +08:00
|
|
|
void HandleTranslationUnit(ASTContext &Ctx) override {
|
2017-09-22 19:11:01 +08:00
|
|
|
handleDecl(Ctx.getTranslationUnitDecl());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void handleDecl(const Decl *D);
|
|
|
|
|
2018-11-02 19:22:22 +08:00
|
|
|
SourceManager &SM;
|
2017-09-22 19:11:01 +08:00
|
|
|
llvm::StringMap<std::string> Index;
|
|
|
|
std::string CurrentFileName;
|
|
|
|
};
|
|
|
|
|
[analyzer][CrossTU][NFC] Generalize to external definitions instead of external functions
Summary: This is just changing naming and documentation to be general about external definitions that can be imported for cross translation unit analysis. There is at least a plan to add VarDecls: D46421
Reviewers: NoQ, xazax.hun, martong, a.sidorin, george.karpenkov, serge-sans-paille
Reviewed By: xazax.hun, martong
Subscribers: mgorny, whisperity, baloghadamsoftware, szepet, rnkovacs, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, cfe-commits
Differential Revision: https://reviews.llvm.org/D56441
llvm-svn: 350852
2019-01-11 01:44:04 +08:00
|
|
|
void MapExtDefNamesConsumer::handleDecl(const Decl *D) {
|
2017-09-22 19:11:01 +08:00
|
|
|
if (!D)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
|
|
|
|
if (FD->isThisDeclarationADefinition()) {
|
|
|
|
if (const Stmt *Body = FD->getBody()) {
|
|
|
|
if (CurrentFileName.empty()) {
|
|
|
|
CurrentFileName =
|
|
|
|
SM.getFileEntryForID(SM.getMainFileID())->tryGetRealPathName();
|
|
|
|
if (CurrentFileName.empty())
|
|
|
|
CurrentFileName = "invalid_file";
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (FD->getLinkageInternal()) {
|
|
|
|
case ExternalLinkage:
|
|
|
|
case VisibleNoLinkage:
|
|
|
|
case UniqueExternalLinkage:
|
2018-11-02 19:22:22 +08:00
|
|
|
if (SM.isInMainFile(Body->getBeginLoc())) {
|
|
|
|
std::string LookupName =
|
|
|
|
CrossTranslationUnitContext::getLookupName(FD);
|
2017-09-22 19:11:01 +08:00
|
|
|
Index[LookupName] = CurrentFileName;
|
2018-11-02 19:22:22 +08:00
|
|
|
}
|
Fix clang -Wimplicit-fallthrough warnings across llvm, NFC
This patch should not introduce any behavior changes. It consists of
mostly one of two changes:
1. Replacing fall through comments with the LLVM_FALLTHROUGH macro
2. Inserting 'break' before falling through into a case block consisting
of only 'break'.
We were already using this warning with GCC, but its warning behaves
slightly differently. In this patch, the following differences are
relevant:
1. GCC recognizes comments that say "fall through" as annotations, clang
doesn't
2. GCC doesn't warn on "case N: foo(); default: break;", clang does
3. GCC doesn't warn when the case contains a switch, but falls through
the outer case.
I will enable the warning separately in a follow-up patch so that it can
be cleanly reverted if necessary.
Reviewers: alexfh, rsmith, lattner, rtrieu, EricWF, bollu
Differential Revision: https://reviews.llvm.org/D53950
llvm-svn: 345882
2018-11-02 03:54:45 +08:00
|
|
|
break;
|
2017-09-22 19:11:01 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (const auto *DC = dyn_cast<DeclContext>(D))
|
|
|
|
for (const Decl *D : DC->decls())
|
|
|
|
handleDecl(D);
|
|
|
|
}
|
|
|
|
|
[analyzer][CrossTU][NFC] Generalize to external definitions instead of external functions
Summary: This is just changing naming and documentation to be general about external definitions that can be imported for cross translation unit analysis. There is at least a plan to add VarDecls: D46421
Reviewers: NoQ, xazax.hun, martong, a.sidorin, george.karpenkov, serge-sans-paille
Reviewed By: xazax.hun, martong
Subscribers: mgorny, whisperity, baloghadamsoftware, szepet, rnkovacs, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, cfe-commits
Differential Revision: https://reviews.llvm.org/D56441
llvm-svn: 350852
2019-01-11 01:44:04 +08:00
|
|
|
class MapExtDefNamesAction : public ASTFrontendAction {
|
2017-09-22 19:11:01 +08:00
|
|
|
protected:
|
|
|
|
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
|
|
|
|
llvm::StringRef) {
|
[analyzer][CrossTU][NFC] Generalize to external definitions instead of external functions
Summary: This is just changing naming and documentation to be general about external definitions that can be imported for cross translation unit analysis. There is at least a plan to add VarDecls: D46421
Reviewers: NoQ, xazax.hun, martong, a.sidorin, george.karpenkov, serge-sans-paille
Reviewed By: xazax.hun, martong
Subscribers: mgorny, whisperity, baloghadamsoftware, szepet, rnkovacs, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, cfe-commits
Differential Revision: https://reviews.llvm.org/D56441
llvm-svn: 350852
2019-01-11 01:44:04 +08:00
|
|
|
return llvm::make_unique<MapExtDefNamesConsumer>(CI.getASTContext());
|
2017-09-22 19:11:01 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static cl::extrahelp CommonHelp(CommonOptionsParser::HelpMessage);
|
|
|
|
|
|
|
|
int main(int argc, const char **argv) {
|
|
|
|
// Print a stack trace if we signal out.
|
|
|
|
sys::PrintStackTraceOnErrorSignal(argv[0], false);
|
|
|
|
PrettyStackTraceProgram X(argc, argv);
|
|
|
|
|
|
|
|
const char *Overview = "\nThis tool collects the USR name and location "
|
[analyzer][CrossTU][NFC] Generalize to external definitions instead of external functions
Summary: This is just changing naming and documentation to be general about external definitions that can be imported for cross translation unit analysis. There is at least a plan to add VarDecls: D46421
Reviewers: NoQ, xazax.hun, martong, a.sidorin, george.karpenkov, serge-sans-paille
Reviewed By: xazax.hun, martong
Subscribers: mgorny, whisperity, baloghadamsoftware, szepet, rnkovacs, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, cfe-commits
Differential Revision: https://reviews.llvm.org/D56441
llvm-svn: 350852
2019-01-11 01:44:04 +08:00
|
|
|
"of external definitions in the source files "
|
2017-09-22 19:11:01 +08:00
|
|
|
"(excluding headers).\n";
|
[analyzer][CrossTU][NFC] Generalize to external definitions instead of external functions
Summary: This is just changing naming and documentation to be general about external definitions that can be imported for cross translation unit analysis. There is at least a plan to add VarDecls: D46421
Reviewers: NoQ, xazax.hun, martong, a.sidorin, george.karpenkov, serge-sans-paille
Reviewed By: xazax.hun, martong
Subscribers: mgorny, whisperity, baloghadamsoftware, szepet, rnkovacs, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, cfe-commits
Differential Revision: https://reviews.llvm.org/D56441
llvm-svn: 350852
2019-01-11 01:44:04 +08:00
|
|
|
CommonOptionsParser OptionsParser(argc, argv, ClangExtDefMapGenCategory,
|
2017-09-22 19:11:01 +08:00
|
|
|
cl::ZeroOrMore, Overview);
|
|
|
|
|
|
|
|
ClangTool Tool(OptionsParser.getCompilations(),
|
|
|
|
OptionsParser.getSourcePathList());
|
2018-11-02 19:22:22 +08:00
|
|
|
|
[analyzer][CrossTU][NFC] Generalize to external definitions instead of external functions
Summary: This is just changing naming and documentation to be general about external definitions that can be imported for cross translation unit analysis. There is at least a plan to add VarDecls: D46421
Reviewers: NoQ, xazax.hun, martong, a.sidorin, george.karpenkov, serge-sans-paille
Reviewed By: xazax.hun, martong
Subscribers: mgorny, whisperity, baloghadamsoftware, szepet, rnkovacs, mikhail.ramalho, Szelethus, donat.nagy, dkrupp, cfe-commits
Differential Revision: https://reviews.llvm.org/D56441
llvm-svn: 350852
2019-01-11 01:44:04 +08:00
|
|
|
return Tool.run(newFrontendActionFactory<MapExtDefNamesAction>().get());
|
2017-09-22 19:11:01 +08:00
|
|
|
}
|