36#include "clang/ASTMatchers/ASTMatchFinder.h"
37#include "clang/ASTMatchers/ASTMatchers.h"
38#include "clang/Basic/SourceManager.h"
39#include "clang/Frontend/FrontendActions.h"
40#include "clang/Lex/Lexer.h"
41#include "clang/Tooling/CommonOptionsParser.h"
42#include "clang/Tooling/Execution.h"
43#include "clang/Tooling/Refactoring.h"
44#include "clang/Tooling/Refactoring/AtomicChange.h"
45#include "clang/Tooling/Tooling.h"
46#include "llvm/Support/CommandLine.h"
47#include "llvm/Support/MemoryBuffer.h"
48#include "llvm/Support/Signals.h"
56class ToolTemplateCallback :
public MatchFinder::MatchCallback {
58 ToolTemplateCallback(ExecutionContext &Context) : Context(Context) {}
60 void run(
const MatchFinder::MatchResult &Result)
override {
65 auto *D = Result.Nodes.getNodeAs<NamedDecl>(
"decl");
68 if (D->getBeginLoc().isValid()) {
69 AtomicChange Change(*Result.SourceManager, D->getBeginLoc());
70 Context.reportResult(Change.getKey(), D->getQualifiedNameAsString());
74 void onStartOfTranslationUnit()
override {
75 Context.reportResult(
"START",
"Start of TU.");
77 void onEndOfTranslationUnit()
override {
78 Context.reportResult(
"END",
"End of TU.");
82 ExecutionContext &Context;
87static cl::extrahelp
CommonHelp(CommonOptionsParser::HelpMessage);
90int main(
int argc,
const char **argv) {
91 llvm::sys::PrintStackTraceOnErrorSignal(argv[0]);
93 auto Executor = clang::tooling::createExecutorFromCommandLineArgs(
97 llvm::errs() << llvm::toString(Executor.takeError()) <<
"\n";
101 ast_matchers::MatchFinder Finder;
102 ToolTemplateCallback Callback(*Executor->get()->getExecutionContext());
110 namedDecl(cxxRecordDecl(), isExpansionInMainFile()).bind(
"decl"),
113 auto Err = Executor->get()->execute(newFrontendActionFactory(&Finder));
115 llvm::errs() << llvm::toString(std::move(Err)) <<
"\n";
117 Executor->get()->getToolResults()->forEachResult(
118 [](llvm::StringRef key, llvm::StringRef value) {
119 llvm::errs() <<
"----" << key.str() <<
"\n" << value.str() <<
"\n";
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Some operations such as code completion produce a set of candidates.