[clang] bfb6c28 - [AST][NFC] Remove temporary ASTTU file from Introspection generation.

Nathan James via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 14 16:15:04 PDT 2021


Author: Nathan James
Date: 2021-04-15T00:14:55+01:00
New Revision: bfb6c2874be830cc6f7595652d23192598edecf0

URL: https://github.com/llvm/llvm-project/commit/bfb6c2874be830cc6f7595652d23192598edecf0
DIFF: https://github.com/llvm/llvm-project/commit/bfb6c2874be830cc6f7595652d23192598edecf0.diff

LOG: [AST][NFC] Remove temporary ASTTU file from Introspection generation.

We can use the Preprocessor to remap this file, cleaning up the cmake code.

Reviewed By: steveire

Differential Revision: https://reviews.llvm.org/D100343

Added: 
    

Modified: 
    clang/lib/Tooling/CMakeLists.txt
    clang/lib/Tooling/DumpTool/ClangSrcLocDump.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Tooling/CMakeLists.txt b/clang/lib/Tooling/CMakeLists.txt
index 3598a44f1305..6d70c8976f03 100644
--- a/clang/lib/Tooling/CMakeLists.txt
+++ b/clang/lib/Tooling/CMakeLists.txt
@@ -78,11 +78,6 @@ else()
 
   set(skip_expensive_processing $<OR:$<CONFIG:Debug>,$<NOT:$<BOOL:${CLANG_TOOLING_BUILD_AST_INTROSPECTION}>>>)
 
-  file(GENERATE OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/ASTTU.cpp
-    CONTENT "
-#include <clang/AST/AST.h>
-")
-
   set(implicitDirs)
   foreach(implicitDir ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES})
     list(APPEND implicitDirs -I ${implicitDir})
@@ -91,12 +86,11 @@ else()
   add_custom_command(
       COMMENT Generate ASTNodeAPI.json
       OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/ASTNodeAPI.json
-      DEPENDS clang-ast-dump clang-resource-headers ${CMAKE_CURRENT_BINARY_DIR}/ASTTU.cpp
+      DEPENDS clang-ast-dump clang-resource-headers
       COMMAND
       $<TARGET_FILE:clang-ast-dump>
         # Skip this in debug mode because parsing AST.h is too slow
         --skip-processing=${skip_expensive_processing}
-        --astheader=${CMAKE_CURRENT_BINARY_DIR}/ASTTU.cpp
         -I ${CMAKE_BINARY_DIR}/lib/clang/${CLANG_VERSION}/include
         -I ${CMAKE_SOURCE_DIR}/../clang/include
         -I ${CMAKE_BINARY_DIR}/tools/clang/include

diff  --git a/clang/lib/Tooling/DumpTool/ClangSrcLocDump.cpp b/clang/lib/Tooling/DumpTool/ClangSrcLocDump.cpp
index 8328977178cc..436d388a99f4 100644
--- a/clang/lib/Tooling/DumpTool/ClangSrcLocDump.cpp
+++ b/clang/lib/Tooling/DumpTool/ClangSrcLocDump.cpp
@@ -14,6 +14,7 @@
 #include "clang/Driver/Tool.h"
 #include "clang/Frontend/CompilerInstance.h"
 #include "clang/Frontend/TextDiagnosticPrinter.h"
+#include "clang/Lex/PreprocessorOptions.h"
 #include "clang/Tooling/Tooling.h"
 #include "llvm/Option/ArgList.h"
 #include "llvm/Support/CommandLine.h"
@@ -30,10 +31,6 @@ static cl::list<std::string> IncludeDirectories(
     "I", cl::desc("Include directories to use while compiling"),
     cl::value_desc("directory"), cl::Required, cl::OneOrMore, cl::Prefix);
 
-static cl::opt<std::string>
-    AstHeaderFile("astheader", cl::desc("AST header to parse API from"),
-                  cl::Required, cl::value_desc("AST header file"));
-
 static cl::opt<bool>
     SkipProcessing("skip-processing",
                    cl::desc("Avoid processing the AST header file"),
@@ -66,6 +63,8 @@ class ASTSrcLocGenerationAction : public clang::ASTFrontendAction {
   ASTSrcLocProcessor Processor;
 };
 
+static const char Filename[] = "ASTTU.cpp";
+
 int main(int argc, const char **argv) {
 
   cl::ParseCommandLineOptions(argc, argv);
@@ -86,7 +85,7 @@ int main(int argc, const char **argv) {
                   [](const std::string &IncDir) { return "-I" + IncDir; });
 
   Args.push_back("-fsyntax-only");
-  Args.push_back(AstHeaderFile);
+  Args.push_back(Filename);
 
   std::vector<const char *> Argv(Args.size(), nullptr);
   llvm::transform(Args, Argv.begin(),
@@ -102,18 +101,23 @@ int main(int argc, const char **argv) {
   // Don't output diagnostics, because common scenarios such as
   // cross-compiling fail with diagnostics.  This is not fatal, but
   // just causes attempts to use the introspection API to return no data.
-  std::string Str;
-  llvm::raw_string_ostream OS(Str);
-  TextDiagnosticPrinter DiagnosticPrinter(OS, &*DiagOpts);
+  TextDiagnosticPrinter DiagnosticPrinter(llvm::nulls(), &*DiagOpts);
   DiagnosticsEngine Diagnostics(
       IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts,
       &DiagnosticPrinter, false);
 
-  FileManager Files(FileSystemOptions(), vfs::getRealFileSystem());
+  auto *OFS = new llvm::vfs::OverlayFileSystem(vfs::getRealFileSystem());
+
+  auto *MemFS = new llvm::vfs::InMemoryFileSystem();
+  OFS->pushOverlay(MemFS);
+  MemFS->addFile(Filename, 0,
+                 MemoryBuffer::getMemBuffer("#include \"clang/AST/AST.h\"\n"));
+
+  auto Files = llvm::makeIntrusiveRefCnt<FileManager>(FileSystemOptions(), OFS);
 
   auto Driver = std::make_unique<driver::Driver>(
       "clang", llvm::sys::getDefaultTargetTriple(), Diagnostics,
-      "ast-api-dump-tool", &Files.getVirtualFileSystem());
+      "ast-api-dump-tool", OFS);
 
   std::unique_ptr<clang::driver::Compilation> Comp(
       Driver->BuildCompilation(llvm::makeArrayRef(Argv)));
@@ -143,12 +147,13 @@ int main(int argc, const char **argv) {
 
   // Suppress "2 errors generated" or similar messages
   Compiler.getDiagnosticOpts().ShowCarets = false;
-  Compiler.createSourceManager(Files);
+  Compiler.createSourceManager(*Files);
+  Compiler.setFileManager(Files.get());
 
   ASTSrcLocGenerationAction ScopedToolAction;
   Compiler.ExecuteAction(ScopedToolAction);
 
-  Files.clearStatCache();
+  Files->clearStatCache();
 
   return 0;
 }


        


More information about the cfe-commits mailing list