[cfe-commits] r110039 - in /cfe/trunk: examples/PrintFunctionNames/PrintFunctionNames.cpp include/clang/Frontend/FrontendAction.h tools/driver/cc1_main.cpp

Daniel Dunbar daniel at zuster.org
Mon Aug 2 08:31:28 PDT 2010


Author: ddunbar
Date: Mon Aug  2 10:31:28 2010
New Revision: 110039

URL: http://llvm.org/viewvc/llvm-project?rev=110039&view=rev
Log:
Frontend: Change PluginASTAction::ParseArgs to take a CompilerInstance object
for use in reporting diagnostics.
 - We don't want to use the Action's own CompilerInstance, because that is only
   initialized during file processing and I like that invariant.

Also, if ParseArgs returns false then abandon execution.

Also, remove unused PluginASTAction::PrintHelp virtual method.

Modified:
    cfe/trunk/examples/PrintFunctionNames/PrintFunctionNames.cpp
    cfe/trunk/include/clang/Frontend/FrontendAction.h
    cfe/trunk/tools/driver/cc1_main.cpp

Modified: cfe/trunk/examples/PrintFunctionNames/PrintFunctionNames.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/examples/PrintFunctionNames/PrintFunctionNames.cpp?rev=110039&r1=110038&r2=110039&view=diff
==============================================================================
--- cfe/trunk/examples/PrintFunctionNames/PrintFunctionNames.cpp (original)
+++ cfe/trunk/examples/PrintFunctionNames/PrintFunctionNames.cpp Mon Aug  2 10:31:28 2010
@@ -15,6 +15,7 @@
 #include "clang/Frontend/FrontendPluginRegistry.h"
 #include "clang/AST/ASTConsumer.h"
 #include "clang/AST/AST.h"
+#include "clang/Frontend/CompilerInstance.h"
 #include "llvm/Support/raw_ostream.h"
 using namespace clang;
 
@@ -29,7 +30,7 @@
         llvm::errs() << "top-level-decl: \"" << ND->getNameAsString() << "\"\n";
     }
   }
-}; 
+};
 
 class PrintFunctionNamesAction : public PluginASTAction {
 protected:
@@ -37,15 +38,26 @@
     return new PrintFunctionsConsumer();
   }
 
-  bool ParseArgs(const std::vector<std::string>& args) {
-    for (unsigned i=0; i<args.size(); ++i)
+  bool ParseArgs(const CompilerInstance &CI,
+                 const std::vector<std::string>& args) {
+    for (unsigned i = 0, e = args.size(); i != e; ++i) {
       llvm::errs() << "PrintFunctionNames arg = " << args[i] << "\n";
+
+      // Example error handling.
+      if (args[i] == "-an-error") {
+        Diagnostic &D = CI.getDiagnostics();
+        unsigned DiagID = D.getCustomDiagID(
+          Diagnostic::Error, "invalid argument '" + args[i] + "'");
+        D.Report(DiagID);
+        return false;
+      }
+    }
     if (args.size() && args[0] == "help")
       PrintHelp(llvm::errs());
 
     return true;
   }
-  void PrintHelp(llvm::raw_ostream& ros) { 
+  void PrintHelp(llvm::raw_ostream& ros) {
     ros << "Help for PrintFunctionNames plugin goes here\n";
   }
 

Modified: cfe/trunk/include/clang/Frontend/FrontendAction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Frontend/FrontendAction.h?rev=110039&r1=110038&r2=110039&view=diff
==============================================================================
--- cfe/trunk/include/clang/Frontend/FrontendAction.h (original)
+++ cfe/trunk/include/clang/Frontend/FrontendAction.h Mon Aug  2 10:31:28 2010
@@ -225,8 +225,14 @@
                                          llvm::StringRef InFile) = 0;
 
 public:
-  virtual bool ParseArgs(const std::vector<std::string>& arg) = 0;
-  virtual void PrintHelp(llvm::raw_ostream&) = 0;
+  /// ParseArgs - Parse the given plugin command line arguments.
+  ///
+  /// \param CI - The compiler instance, for use in reporting diagnostics.
+  /// \return True if the parsing succeeded; otherwise the plugin will be
+  /// destroyed and no action run. The plugin is responsible for using the
+  /// CompilerInstance's Diagnostic object to report errors.
+  virtual bool ParseArgs(const CompilerInstance &CI,
+                         const std::vector<std::string> &arg) = 0;
 };
 
 /// PreprocessorFrontendAction - Abstract base class to use for preprocessor

Modified: cfe/trunk/tools/driver/cc1_main.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/driver/cc1_main.cpp?rev=110039&r1=110038&r2=110039&view=diff
==============================================================================
--- cfe/trunk/tools/driver/cc1_main.cpp (original)
+++ cfe/trunk/tools/driver/cc1_main.cpp Mon Aug  2 10:31:28 2010
@@ -83,14 +83,14 @@
   case ParseSyntaxOnly:        return new SyntaxOnlyAction();
 
   case PluginAction: {
-
     for (FrontendPluginRegistry::iterator it =
            FrontendPluginRegistry::begin(), ie = FrontendPluginRegistry::end();
          it != ie; ++it) {
       if (it->getName() == CI.getFrontendOpts().ActionName) {
-        PluginASTAction* plugin = it->instantiate();
-        plugin->ParseArgs(CI.getFrontendOpts().PluginArgs);
-        return plugin;
+        llvm::OwningPtr<PluginASTAction> P(it->instantiate());
+        if (!P->ParseArgs(CI, CI.getFrontendOpts().PluginArgs))
+          return 0;
+        return P.take();
       }
     }
 
@@ -287,7 +287,7 @@
   // If any timers were active but haven't been destroyed yet, print their
   // results now.  This happens in -disable-free mode.
   llvm::TimerGroup::printAll(llvm::errs());
-  
+
   // When running with -disable-free, don't do any destruction or shutdown.
   if (Clang->getFrontendOpts().DisableFree) {
     if (Clang->getFrontendOpts().ShowStats)





More information about the cfe-commits mailing list