Skip to content

Instantly share code, notes, and snippets.

@realincubus

realincubus/clang plugin patch Secret

Last active Aug 29, 2015
Embed
What would you like to do?
diff --git a/examples/PrintFunctionNames/CMakeLists.txt b/examples/PrintFunctionNames/CMakeLists.txt
index e700281..e104e59 100644
--- a/examples/PrintFunctionNames/CMakeLists.txt
+++ b/examples/PrintFunctionNames/CMakeLists.txt
@@ -10,6 +10,7 @@ if( NOT MSVC ) # MSVC mangles symbols differently, and
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}
diff --git a/examples/PrintFunctionNames/PrintFunctionNames.cpp b/examples/PrintFunctionNames/PrintFunctionNames.cpp
index e8a361d..c7ae007 100644
--- a/examples/PrintFunctionNames/PrintFunctionNames.cpp
+++ b/examples/PrintFunctionNames/PrintFunctionNames.cpp
@@ -23,11 +23,23 @@ namespace {
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 @@ class PrintFunctionNamesAction : public PluginASTAction {
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,
diff --git a/include/clang/Frontend/CompilerInstance.h b/include/clang/Frontend/CompilerInstance.h
index 40e3069..b83ffd9 100644
--- a/include/clang/Frontend/CompilerInstance.h
+++ b/include/clang/Frontend/CompilerInstance.h
@@ -709,6 +709,9 @@ public:
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
diff --git a/lib/Frontend/ASTUnit.cpp b/lib/Frontend/ASTUnit.cpp
index 963752a..8d2c57d 100644
--- a/lib/Frontend/ASTUnit.cpp
+++ b/lib/Frontend/ASTUnit.cpp
@@ -1130,6 +1130,8 @@ bool ASTUnit::Parse(std::unique_ptr<llvm::MemoryBuffer> OverrideMainBuffer) {
llvm::CrashRecoveryContextCleanupRegistrar<TopLevelDeclTrackerAction>
ActCleanup(Act.get());
+ Clang->loadPlugins();
+
if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0]))
goto error;
diff --git a/lib/Frontend/CompilerInstance.cpp b/lib/Frontend/CompilerInstance.cpp
index 923881c..e6627ae 100644
--- a/lib/Frontend/CompilerInstance.cpp
+++ b/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 @@ CompilerInstance::lookupMissingImports(StringRef Name,
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;
+ }
+}
+
diff --git a/lib/FrontendTool/ExecuteCompilerInvocation.cpp b/lib/FrontendTool/ExecuteCompilerInvocation.cpp
index 79cf004..aefa527 100644
--- a/lib/FrontendTool/ExecuteCompilerInvocation.cpp
+++ b/lib/FrontendTool/ExecuteCompilerInvocation.cpp
@@ -180,14 +180,7 @@ bool clang::ExecuteCompilerInvocation(CompilerInstance *Clang) {
}
// 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.
//
diff --git a/tools/libclang/libclang.exports b/tools/libclang/libclang.exports
index 5a029f7..ca3d67d 100644
--- a/tools/libclang/libclang.exports
+++ b/tools/libclang/libclang.exports
@@ -301,3 +301,20 @@ clang_VirtualFileOverlay_create
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment