2016-02-14 14:39:11 +08:00
|
|
|
//===-- core_main.cpp - Core Index Tool testbed ---------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Frontend/ASTUnit.h"
|
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Frontend/CompilerInvocation.h"
|
|
|
|
#include "clang/Frontend/FrontendAction.h"
|
|
|
|
#include "clang/Index/IndexingAction.h"
|
|
|
|
#include "clang/Index/IndexDataConsumer.h"
|
|
|
|
#include "clang/Index/USRGeneration.h"
|
2016-02-15 06:30:14 +08:00
|
|
|
#include "clang/Index/CodegenNameGenerator.h"
|
2016-02-14 14:39:11 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Signals.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include "llvm/Support/PrettyStackTrace.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace clang::index;
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
extern "C" int indextest_core_main(int argc, const char **argv);
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
enum class ActionType {
|
|
|
|
None,
|
|
|
|
PrintSourceSymbols,
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace options {
|
|
|
|
|
|
|
|
static cl::OptionCategory IndexTestCoreCategory("index-test-core options");
|
|
|
|
|
|
|
|
static cl::opt<ActionType>
|
|
|
|
Action(cl::desc("Action:"), cl::init(ActionType::None),
|
|
|
|
cl::values(
|
|
|
|
clEnumValN(ActionType::PrintSourceSymbols,
|
2016-10-09 03:41:06 +08:00
|
|
|
"print-source-symbols", "Print symbols from source")),
|
2016-02-14 14:39:11 +08:00
|
|
|
cl::cat(IndexTestCoreCategory));
|
|
|
|
|
|
|
|
static cl::extrahelp MoreHelp(
|
|
|
|
"\nAdd \"-- <compiler arguments>\" at the end to setup the compiler "
|
|
|
|
"invocation\n"
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
static void printSymbolInfo(SymbolInfo SymInfo, raw_ostream &OS);
|
|
|
|
static void printSymbolNameAndUSR(const Decl *D, ASTContext &Ctx,
|
|
|
|
raw_ostream &OS);
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class PrintIndexDataConsumer : public IndexDataConsumer {
|
|
|
|
raw_ostream &OS;
|
2016-02-15 06:30:14 +08:00
|
|
|
std::unique_ptr<CodegenNameGenerator> CGNameGen;
|
2016-02-14 14:39:11 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
PrintIndexDataConsumer(raw_ostream &OS) : OS(OS) {
|
|
|
|
}
|
|
|
|
|
2016-02-15 06:30:14 +08:00
|
|
|
void initialize(ASTContext &Ctx) override {
|
|
|
|
CGNameGen.reset(new CodegenNameGenerator(Ctx));
|
|
|
|
}
|
|
|
|
|
2016-02-14 14:39:11 +08:00
|
|
|
bool handleDeclOccurence(const Decl *D, SymbolRoleSet Roles,
|
|
|
|
ArrayRef<SymbolRelation> Relations,
|
|
|
|
FileID FID, unsigned Offset,
|
|
|
|
ASTNodeInfo ASTNode) override {
|
|
|
|
ASTContext &Ctx = D->getASTContext();
|
|
|
|
SourceManager &SM = Ctx.getSourceManager();
|
|
|
|
|
|
|
|
unsigned Line = SM.getLineNumber(FID, Offset);
|
|
|
|
unsigned Col = SM.getColumnNumber(FID, Offset);
|
|
|
|
OS << Line << ':' << Col << " | ";
|
|
|
|
|
|
|
|
printSymbolInfo(getSymbolInfo(D), OS);
|
|
|
|
OS << " | ";
|
|
|
|
|
|
|
|
printSymbolNameAndUSR(D, Ctx, OS);
|
|
|
|
OS << " | ";
|
|
|
|
|
2016-02-15 06:30:14 +08:00
|
|
|
if (CGNameGen->writeName(D, OS))
|
|
|
|
OS << "<no-cgname>";
|
|
|
|
OS << " | ";
|
|
|
|
|
2016-02-14 14:39:11 +08:00
|
|
|
printSymbolRoles(Roles, OS);
|
|
|
|
OS << " | ";
|
|
|
|
|
|
|
|
OS << "rel: " << Relations.size() << '\n';
|
|
|
|
|
|
|
|
for (auto &SymRel : Relations) {
|
|
|
|
OS << '\t';
|
|
|
|
printSymbolRoles(SymRel.Roles, OS);
|
|
|
|
OS << " | ";
|
|
|
|
printSymbolNameAndUSR(SymRel.RelatedSymbol, Ctx, OS);
|
|
|
|
OS << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2016-02-29 15:56:07 +08:00
|
|
|
|
|
|
|
bool handleModuleOccurence(const ImportDecl *ImportD, SymbolRoleSet Roles,
|
|
|
|
FileID FID, unsigned Offset) override {
|
|
|
|
ASTContext &Ctx = ImportD->getASTContext();
|
|
|
|
SourceManager &SM = Ctx.getSourceManager();
|
|
|
|
|
|
|
|
unsigned Line = SM.getLineNumber(FID, Offset);
|
|
|
|
unsigned Col = SM.getColumnNumber(FID, Offset);
|
|
|
|
OS << Line << ':' << Col << " | ";
|
|
|
|
|
|
|
|
printSymbolInfo(getSymbolInfo(ImportD), OS);
|
|
|
|
OS << " | ";
|
|
|
|
|
|
|
|
OS << ImportD->getImportedModule()->getFullModuleName() << " | ";
|
|
|
|
|
|
|
|
printSymbolRoles(Roles, OS);
|
|
|
|
OS << " |\n";
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2016-02-14 14:39:11 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Print Source Symbols
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
static bool printSourceSymbols(ArrayRef<const char *> Args) {
|
|
|
|
SmallVector<const char *, 4> ArgsWithProgName;
|
|
|
|
ArgsWithProgName.push_back("clang");
|
|
|
|
ArgsWithProgName.append(Args.begin(), Args.end());
|
|
|
|
IntrusiveRefCntPtr<DiagnosticsEngine>
|
|
|
|
Diags(CompilerInstance::createDiagnostics(new DiagnosticOptions));
|
|
|
|
IntrusiveRefCntPtr<CompilerInvocation>
|
|
|
|
CInvok(createInvocationFromCommandLine(ArgsWithProgName, Diags));
|
|
|
|
if (!CInvok)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
auto DataConsumer = std::make_shared<PrintIndexDataConsumer>(outs());
|
|
|
|
IndexingOptions IndexOpts;
|
|
|
|
std::unique_ptr<FrontendAction> IndexAction;
|
2016-02-14 14:53:20 +08:00
|
|
|
IndexAction = createIndexingAction(DataConsumer, IndexOpts,
|
|
|
|
/*WrappedAction=*/nullptr);
|
2016-02-14 14:39:11 +08:00
|
|
|
|
|
|
|
auto PCHContainerOps = std::make_shared<PCHContainerOperations>();
|
2016-02-14 21:18:06 +08:00
|
|
|
std::unique_ptr<ASTUnit> Unit(ASTUnit::LoadFromCompilerInvocationAction(
|
|
|
|
CInvok.get(), PCHContainerOps, Diags, IndexAction.get()));
|
2016-02-14 14:39:11 +08:00
|
|
|
|
|
|
|
if (!Unit)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Helper Utils
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
static void printSymbolInfo(SymbolInfo SymInfo, raw_ostream &OS) {
|
|
|
|
OS << getSymbolKindString(SymInfo.Kind);
|
2016-11-12 07:49:55 +08:00
|
|
|
if (SymInfo.Properties) {
|
2016-04-22 15:21:10 +08:00
|
|
|
OS << '(';
|
2016-11-12 07:49:55 +08:00
|
|
|
printSymbolProperties(SymInfo.Properties, OS);
|
2016-04-22 15:21:10 +08:00
|
|
|
OS << ')';
|
2016-02-14 14:39:11 +08:00
|
|
|
}
|
|
|
|
OS << '/' << getSymbolLanguageString(SymInfo.Lang);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void printSymbolNameAndUSR(const Decl *D, ASTContext &Ctx,
|
|
|
|
raw_ostream &OS) {
|
2016-02-15 09:32:36 +08:00
|
|
|
if (printSymbolName(D, Ctx.getLangOpts(), OS)) {
|
2016-02-14 14:39:11 +08:00
|
|
|
OS << "<no-name>";
|
|
|
|
}
|
|
|
|
OS << " | ";
|
|
|
|
|
|
|
|
SmallString<256> USRBuf;
|
|
|
|
if (generateUSRForDecl(D, USRBuf)) {
|
|
|
|
OS << "<no-usr>";
|
|
|
|
} else {
|
|
|
|
OS << USRBuf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Command line processing.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
int indextest_core_main(int argc, const char **argv) {
|
2016-06-09 08:53:41 +08:00
|
|
|
sys::PrintStackTraceOnErrorSignal(argv[0]);
|
2016-02-14 14:39:11 +08:00
|
|
|
PrettyStackTraceProgram X(argc, argv);
|
|
|
|
|
2016-06-09 08:53:41 +08:00
|
|
|
assert(argv[1] == StringRef("core"));
|
|
|
|
++argv;
|
|
|
|
--argc;
|
|
|
|
|
2016-02-14 14:39:11 +08:00
|
|
|
std::vector<const char *> CompArgs;
|
2016-02-14 15:08:31 +08:00
|
|
|
const char **DoubleDash = std::find(argv, argv + argc, StringRef("--"));
|
2016-02-14 14:39:11 +08:00
|
|
|
if (DoubleDash != argv + argc) {
|
|
|
|
CompArgs = std::vector<const char *>(DoubleDash + 1, argv + argc);
|
|
|
|
argc = DoubleDash - argv;
|
|
|
|
}
|
|
|
|
|
|
|
|
cl::HideUnrelatedOptions(options::IndexTestCoreCategory);
|
|
|
|
cl::ParseCommandLineOptions(argc, argv, "index-test-core");
|
|
|
|
|
|
|
|
if (options::Action == ActionType::None) {
|
|
|
|
errs() << "error: action required; pass '-help' for options\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options::Action == ActionType::PrintSourceSymbols) {
|
|
|
|
if (CompArgs.empty()) {
|
|
|
|
errs() << "error: missing compiler args; pass '-- <compiler arguments>'\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return printSourceSymbols(CompArgs);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|