The goal is to enable tools that use libclang to display additional errors
and warnings.
Normally libclang does not load any plugins when its parsing code through
clang_parseTranslationUnit.
This patch moves the plugin loading code from ExecuteCompilerInvocation to
the class CompilerInstance and adds the method calls to ASTUnit::Parse and
ExecuteCompilerInvocation.
I additionally added the symbols that are needed for the plugin to link
against libclang to tools/libclang/libclang.exports.

http://reviews.llvm.org/D5611

Files:
  examples/PrintFunctionNames/CMakeLists.txt
  examples/PrintFunctionNames/PrintFunctionNames.cpp
  include/clang/Frontend/CompilerInstance.h
  lib/Frontend/ASTUnit.cpp
  lib/Frontend/CompilerInstance.cpp
  lib/FrontendTool/ExecuteCompilerInvocation.cpp
  tools/libclang/libclang.exports
Index: examples/PrintFunctionNames/CMakeLists.txt
===================================================================
--- examples/PrintFunctionNames/CMakeLists.txt
+++ examples/PrintFunctionNames/CMakeLists.txt
@@ -10,6 +10,7 @@
 endif()
 
 add_llvm_loadable_module(PrintFunctionNames PrintFunctionNames.cpp)
+target_link_libraries( PrintFunctionNames libclang )
 
 if(LLVM_ENABLE_PLUGINS AND (WIN32 OR CYGWIN))
   target_link_libraries(PrintFunctionNames ${cmake_2_8_12_PRIVATE}
Index: examples/PrintFunctionNames/PrintFunctionNames.cpp
===================================================================
--- examples/PrintFunctionNames/PrintFunctionNames.cpp
+++ examples/PrintFunctionNames/PrintFunctionNames.cpp
@@ -23,11 +23,23 @@
 
 class PrintFunctionsConsumer : public ASTConsumer {
 public:
+  PrintFunctionsConsumer( const CompilerInstance& _CI ):
+    CI(_CI)
+  {
+    
+  }
+  const CompilerInstance& CI;
   virtual bool HandleTopLevelDecl(DeclGroupRef DG) {
+    DiagnosticsEngine& DE = CI.getDiagnostics();
     for (DeclGroupRef::iterator i = DG.begin(), e = DG.end(); i != e; ++i) {
       const Decl *D = *i;
-      if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
+      if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)){
         llvm::errs() << "top-level-decl: \"" << ND->getNameAsString() << "\"\n";
+	unsigned DiagID = DE.getCustomDiagID(DiagnosticsEngine::Error,
+	    "functions are bad '%0'");
+	DE.Report( ND->getLocStart(), DiagID ) 
+	  << ND->getNameAsString();
+      }
     }
 
     return true;
@@ -38,7 +50,7 @@
 protected:
   std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI,
                                                  llvm::StringRef) {
-    return llvm::make_unique<PrintFunctionsConsumer>();
+    return llvm::make_unique<PrintFunctionsConsumer>(CI);
   }
 
   bool ParseArgs(const CompilerInstance &CI,
Index: include/clang/Frontend/CompilerInstance.h
===================================================================
--- include/clang/Frontend/CompilerInstance.h
+++ include/clang/Frontend/CompilerInstance.h
@@ -709,6 +709,9 @@
   void addDependencyCollector(std::shared_ptr<DependencyCollector> Listener) {
     DependencyCollectors.push_back(std::move(Listener));
   }
+
+  /// load plugins that were added by -load
+  void loadPlugins();
 };
 
 } // end namespace clang
Index: lib/Frontend/ASTUnit.cpp
===================================================================
--- lib/Frontend/ASTUnit.cpp
+++ lib/Frontend/ASTUnit.cpp
@@ -1130,6 +1130,8 @@
   llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
     ActCleanup(Act.get());
 
+  Clang->loadPlugins();
+
   if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0]))
     goto error;
 
Index: lib/Frontend/CompilerInstance.cpp
===================================================================
--- lib/Frontend/CompilerInstance.cpp
+++ lib/Frontend/CompilerInstance.cpp
@@ -45,6 +45,7 @@
 #include "llvm/Support/Signals.h"
 #include "llvm/Support/Timer.h"
 #include "llvm/Support/raw_ostream.h"
+#include "llvm/Support/DynamicLibrary.h"
 #include <sys/stat.h>
 #include <system_error>
 #include <time.h>
@@ -1619,3 +1620,16 @@
   return false;
 }
 void CompilerInstance::resetAndLeakSema() { BuryPointer(takeSema()); }
+
+void 
+CompilerInstance::loadPlugins(){
+  // Load any requested plugins.
+  for (unsigned i = 0, e = getFrontendOpts().Plugins.size(); i != e; ++i) {
+    const std::string &Path = getFrontendOpts().Plugins[i];
+    std::string Error;
+    if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error))
+      getDiagnostics().Report(diag::err_fe_unable_to_load_plugin)
+        << Path << Error;
+  }
+}
+
Index: lib/FrontendTool/ExecuteCompilerInvocation.cpp
===================================================================
--- lib/FrontendTool/ExecuteCompilerInvocation.cpp
+++ lib/FrontendTool/ExecuteCompilerInvocation.cpp
@@ -180,14 +180,7 @@
   }
 
   // Load any requested plugins.
-  for (unsigned i = 0,
-         e = Clang->getFrontendOpts().Plugins.size(); i != e; ++i) {
-    const std::string &Path = Clang->getFrontendOpts().Plugins[i];
-    std::string Error;
-    if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Path.c_str(), &Error))
-      Clang->getDiagnostics().Report(diag::err_fe_unable_to_load_plugin)
-        << Path << Error;
-  }
+  Clang->loadPlugins();
 
   // Honor -mllvm.
   //
Index: tools/libclang/libclang.exports
===================================================================
--- tools/libclang/libclang.exports
+++ tools/libclang/libclang.exports
@@ -301,3 +301,20 @@
 clang_VirtualFileOverlay_dispose
 clang_VirtualFileOverlay_setCaseSensitivity
 clang_VirtualFileOverlay_writeToBuffer
+_ZN5clang15PluginASTAction6anchorEv
+_ZN4llvm11raw_ostream5writeEPKcm
+_ZN4llvm4errsEv
+_ZN5clang11ASTConsumer33HandleTopLevelDeclInObjCContainerENS_12DeclGroupRefE
+_ZN5clang14FrontendActionD2Ev
+_ZN5clang13DiagnosticIDs15getCustomDiagIDENS0_5LevelEN4llvm9StringRefE
+_ZN5clang17ASTFrontendAction13ExecuteActionEv
+_ZN5clang11ASTConsumer24HandleImplicitImportDeclEPNS_10ImportDeclE
+_ZN5clang14FrontendAction22shouldEraseOutputFilesEv
+_ZNK5clang15DeclarationName11getAsStringEv
+_ZN5clang17DiagnosticsEngine21EmitCurrentDiagnosticEb
+_ZN5clang11ASTConsumer21HandleInterestingDeclENS_12DeclGroupRefE
+_ZN5clang14FrontendActionC2Ev
+_ZN4llvm8RegistryIN5clang15PluginASTActionENS_14RegistryTraitsIS2_EEE4nodeC1ERKNS_19SimpleRegistryEntryIS2_EE
+_ZTVN5clang15PluginASTActionE
+_ZTVN5clang17ASTFrontendActionE
+_ZTVN5clang11ASTConsumerE
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to