2401llvm,编译clang的一个最小xmake示例

add_rules("mode.release")
target("bb")
    add_files("bb.cpp")
    set_languages("c++latest")
    add_includedirs("C:/vs2022/VC/Tools/MSVC/14.37.32822/include")
    add_includedirs("G:/llvm2/clang/include")
    add_includedirs("G:/llvm2/llvm/include")
    add_includedirs("G:/llvm2/build/include")
    add_includedirs("G:/llvm2/build/tools/clang/include")
    add_linkdirs("G:/llvm2/build/Release/lib")
    add_linkdirs("E:/path")
    set_targetdir('.')
    add_links("legacy_stdio_definitions","WS2_32","Ole32","libucrt","user32","kernel32","user32","gdi32","winspool","comdlg32","advapi32","shell32","ole32","oleaut32","uuid","odbc32","odbccp32","libvcruntime","ws2_32","libcmt","libcpmt","libcpmt1","atls","WindowsApp","常用","gdiplus","简图","简图加","minidocx","pugixml","zip","cpr","libcurl","Wldap32","OpenGL32","version",
    
"clang-repl",
"clangAnalysis",
"clangAnalysisFlowSensitive",
"clangAnalysisFlowSensitiveModels",
"clangAPINotes",
"clangARCMigrate",
"clangAST",
"clangASTMatchers",
"clangBasic",
"clangCodeGen",
"clangCrossTU",
"clangDependencyScanning",
"clangDirectoryWatcher",
"clangDriver",
"clangDynamicASTMatchers",
"clangEdit",
"clangExtractAPI",
"clangFormat",
"clangFrontend",
"clangFrontendTool",
"clangHandleCXX",
"clangHandleLLVM",
"clangIndex",
"clangIndexSerialization",
"clangInterpreter",
"clangLex",
"clangParse",
"clangRewrite",
"clangRewriteFrontend",
"clangSema",
"clangSerialization",
"clangStaticAnalyzerCheckers",
"clangStaticAnalyzerCore",
"clangStaticAnalyzerFrontend",
"clangSupport",
"clangTesting",
"clangTooling",
"clangToolingASTDiff",
"clangToolingCore",
"clangToolingInclusions",
"clangToolingInclusionsStdlib",
"clangToolingRefactoring",
"clangToolingSyntax",
"clangTransformer",
"DynamicLibraryLib",
"Kaleidoscope-Ch4",
"Kaleidoscope-Ch5",
"Kaleidoscope-Ch6",
"Kaleidoscope-Ch7",
"Kaleidoscope-Ch8",
"Kaleidoscope-Ch9",
"libclang",
"lldCOFF",
"lldCommon",
"lldELF",
"lldMachO",
"lldMinGW",
"lldWasm",
"LLVM-C",
"LLVMAArch64AsmParser",
"LLVMAArch64CodeGen",
"LLVMAArch64Desc",
"LLVMAArch64Disassembler",
"LLVMAArch64Info",
"LLVMAArch64Utils",
"LLVMAggressiveInstCombine",
"LLVMAMDGPUAsmParser",
"LLVMAMDGPUCodeGen",
"LLVMAMDGPUDesc",
"LLVMAMDGPUDisassembler",
"LLVMAMDGPUInfo",
"LLVMAMDGPUTargetMCA",
"LLVMAMDGPUUtils",
"LLVMAnalysis",
"LLVMARMAsmParser",
"LLVMARMCodeGen",
"LLVMARMDesc",
"LLVMARMDisassembler",
"LLVMARMInfo",
"LLVMARMUtils",
"LLVMAsmParser",
"LLVMAsmPrinter",
"LLVMAVRAsmParser",
"LLVMAVRCodeGen",
"LLVMAVRDesc",
"LLVMAVRDisassembler",
"LLVMAVRInfo",
"LLVMBinaryFormat",
"LLVMBitReader",
"LLVMBitstreamReader",
"LLVMBitWriter",
"LLVMBPFAsmParser",
"LLVMBPFCodeGen",
"LLVMBPFDesc",
"LLVMBPFDisassembler",
"LLVMBPFInfo",
"LLVMCFGuard",
"LLVMCFIVerify",
"LLVMCodeGen",
"LLVMCodeGenTypes",
"LLVMCore",
"LLVMCoroutines",
"LLVMCoverage",
"LLVMDebugInfoBTF",
"LLVMDebugInfoCodeView",
"LLVMDebuginfod",
"LLVMDebugInfoDWARF",
"LLVMDebugInfoGSYM",
"LLVMDebugInfoLogicalView",
"LLVMDebugInfoMSF",
"LLVMDebugInfoPDB",
"LLVMDemangle",
"LLVMDiff",
"LLVMDlltoolDriver",
"LLVMDWARFLinker",
"LLVMDWARFLinkerParallel",
"LLVMDWP",
"LLVMExecutionEngine",
"LLVMExegesis",
"LLVMExegesisAArch64",
"LLVMExegesisMips",
"LLVMExegesisPowerPC",
"LLVMExegesisX86",
"LLVMExtensions",
"LLVMFileCheck",
"LLVMFrontendHLSL",
"LLVMFrontendOpenACC",
"LLVMFrontendOpenMP",
"LLVMFuzzerCLI",
"LLVMFuzzMutate",
"LLVMGlobalISel",
"LLVMHexagonAsmParser",
"LLVMHexagonCodeGen",
"LLVMHexagonDesc",
"LLVMHexagonDisassembler",
"LLVMHexagonInfo",
"LLVMInstCombine",
"LLVMInstrumentation",
"LLVMInterfaceStub",
"LLVMInterpreter",
"LLVMipo",
"LLVMIRPrinter",
"LLVMIRReader",
"LLVMJITLink",
"LLVMLanaiAsmParser",
"LLVMLanaiCodeGen",
"LLVMLanaiDesc",
"LLVMLanaiDisassembler",
"LLVMLanaiInfo",
"LLVMLibDriver",
"LLVMLineEditor",
"LLVMLinker",
"LLVMLTO",
"LLVMMC",
"LLVMMCA",
"LLVMMCDisassembler",
"LLVMMCJIT",
"LLVMMCParser",
"LLVMMIRParser",
"LLVMNVPTXCodeGen",
"LLVMNVPTXDesc",
"LLVMNVPTXInfo",
"LLVMObject",
"LLVMObjectYAML",
"LLVMOption",
"LLVMOrcJIT",
"LLVMOrcShared",
"LLVMOrcTargetProcess",
"LLVMPasses",
"LLVMProfileData",
"LLVMRemarks",
"LLVMRuntimeDyld",
"LLVMScalarOpts",
"LLVMSelectionDAG",
"LLVMSupport",
"LLVMSymbolize",
"LLVMTableGen",
"LLVMTableGenCommon",
"LLVMTableGenGlobalISel",
"LLVMTarget",
"LLVMTargetParser",
"LLVMTextAPI",
"LLVMTransformUtils",
"LLVMVEAsmParser",
"LLVMVECodeGen",
"LLVMVectorize",
"LLVMVEDesc",
"LLVMVEDisassembler",
"LLVMVEInfo",
"LLVMWebAssemblyAsmParser",
"LLVMWebAssemblyCodeGen",
"LLVMWebAssemblyDesc",
"LLVMWebAssemblyDisassembler",
"LLVMWebAssemblyInfo",
"LLVMWebAssemblyUtils",
"LLVMWindowsDriver",
"LLVMWindowsManifest",
"LLVMX86AsmParser",
"LLVMX86CodeGen",
"LLVMX86Desc",
"LLVMX86Disassembler",
"LLVMX86Info",
"LLVMX86TargetMCA",
"LLVMXCoreCodeGen",
"LLVMXCoreDesc",
"LLVMXCoreDisassembler",
"LLVMXCoreInfo",
"LLVMXRay",
"LTO",
"Remarks",
    "glfw3")
    
    --输出在当前目录


只编译一个最小的"bb.cpp"文件.代码如下:

#include <常用>

#include "clang/AST/ASTConsumer.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendAction.h"
#include "clang/Tooling/Tooling.h"

using namespace clang;
class FindNamedClassVisitor
  : public RecursiveASTVisitor<FindNamedClassVisitor> {
public:
  explicit FindNamedClassVisitor(ASTContext *Context)
    : Context(Context) {}
  bool VisitCXXRecordDecl(CXXRecordDecl *Declaration) {
    if (Declaration->getQualifiedNameAsString() == "n::m::C") {
      FullSourceLoc FullLocation = Context->getFullLoc(Declaration->getBeginLoc());
      if (FullLocation.isValid())
        llvm::outs() << "Found declaration at "
     << FullLocation.getSpellingLineNumber() << ":"
     << FullLocation.getSpellingColumnNumber() << "
";
    }
    return true;
  }
private:
  ASTContext *Context;
};
class FindNamedClassConsumer : public clang::ASTConsumer {
public:
  explicit FindNamedClassConsumer(ASTContext *Context)
    : Visitor(Context) {}
  virtual void HandleTranslationUnit(clang::ASTContext &Context) {
    Visitor.TraverseDecl(Context.getTranslationUnitDecl());
  }
private:
  FindNamedClassVisitor Visitor;
};
class FindNamedClassAction : public clang::ASTFrontendAction {
public:
  virtual std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(
    clang::CompilerInstance &Compiler, llvm::StringRef InFile) {
    return std::make_unique<FindNamedClassConsumer>(&Compiler.getASTContext());
  }
};
int main() {
    串 a{"aa.cpp"},b;
    至冲(a,b);

    clang::tooling::runToolOnCode(std::make_unique<FindNamedClassAction>(), b);
}

这里,还需要编译命令.json.