This patch adds an input file name parameter to ArgumentAdjuster's Adjust 
function in Tooling, in order to allow support of changes to a compile's 
arguments based on the file name.

I'm in a testing phase for a new feature for modularize which allows you to add 
dependencies to the header files in the input header list.  In using modularize 
for a real-world set of headers, I found that some headers needed other headers 
to be included first, as opposed to including them themselves or relying on a 
master header to include the subset of headers in the right order.  With this 
patch, I can create an ArgumentAdjuster derivation that's given a map with the 
dependencies keyed on the file name, and have the Adjust function look up the 
dependencies using the input file name and then add -include options to first 
include the depended-on headers.  This seemed easier than other changes or 
derivations that would need to be done.

Thanks.

-John


Index: include/clang/Tooling/ArgumentsAdjusters.h
===================================================================
--- include/clang/Tooling/ArgumentsAdjusters.h  (revision 188102)
+++ include/clang/Tooling/ArgumentsAdjusters.h  (working copy)
@@ -16,6 +16,7 @@
 #ifndef LLVM_CLANG_TOOLING_ARGUMENTSADJUSTERS_H
 #define LLVM_CLANG_TOOLING_ARGUMENTSADJUSTERS_H
 
+#include "llvm/ADT/StringRef.h"
 #include <string>
 #include <vector>
 
@@ -36,10 +37,12 @@
 public:
   /// \brief Returns adjusted command line arguments.
   ///
+  /// \param InputFile Input file.
   /// \param Args Input sequence of command line arguments.
   ///
   /// \returns Modified sequence of command line arguments.
-  virtual CommandLineArguments Adjust(const CommandLineArguments &Args) = 0;
+  virtual CommandLineArguments Adjust(llvm::StringRef InputFile,
+                                      const CommandLineArguments &Args) = 0;
   virtual ~ArgumentsAdjuster() {
   }
 };
@@ -49,13 +52,15 @@
 /// This class implements ArgumentsAdjuster interface and converts input
 /// command line arguments to the "syntax check only" variant.
 class ClangSyntaxOnlyAdjuster : public ArgumentsAdjuster {
-  virtual CommandLineArguments Adjust(const CommandLineArguments &Args);
+  virtual CommandLineArguments Adjust(llvm::StringRef InputFile,
+                                      const CommandLineArguments &Args);
 };
 
 /// \brief An argument adjuster which removes output-related command line
 /// arguments.
 class ClangStripOutputAdjuster : public ArgumentsAdjuster {
-  virtual CommandLineArguments Adjust(const CommandLineArguments &Args);
+  virtual CommandLineArguments Adjust(llvm::StringRef InputFile,
+                                      const CommandLineArguments &Args);
 };
 
 } // end namespace tooling
Index: tools/clang-check/ClangCheck.cpp
===================================================================
--- tools/clang-check/ClangCheck.cpp    (revision 188102)
+++ tools/clang-check/ClangCheck.cpp    (working copy)
@@ -146,7 +146,8 @@
   }
 
   virtual CommandLineArguments
-  Adjust(const CommandLineArguments &Args) LLVM_OVERRIDE {
+  Adjust(llvm::StringRef InputFile,
+         const CommandLineArguments &Args) LLVM_OVERRIDE {
     CommandLineArguments Return(Args);
 
     CommandLineArguments::iterator I;
Index: unittests/Tooling/ToolingTest.cpp
===================================================================
--- unittests/Tooling/ToolingTest.cpp   (revision 188102)
+++ unittests/Tooling/ToolingTest.cpp   (working copy)
@@ -196,11 +196,14 @@
 struct CheckSyntaxOnlyAdjuster: public ArgumentsAdjuster {
   bool &Found;
   bool &Ran;
+  std::string &File;
 
-  CheckSyntaxOnlyAdjuster(bool &Found, bool &Ran) : Found(Found), Ran(Ran) { }
+  CheckSyntaxOnlyAdjuster(bool &Found, bool &Ran, std::string &File)
+    : Found(Found), Ran(Ran), File(File) { }
 
   virtual CommandLineArguments
-  Adjust(const CommandLineArguments &Args) LLVM_OVERRIDE {
+  Adjust(llvm::StringRef InputFile,
+         const CommandLineArguments &Args) LLVM_OVERRIDE {
     Ran = true;
     for (unsigned I = 0, E = Args.size(); I != E; ++I) {
       if (Args[I] == "-fsyntax-only") {
@@ -208,6 +211,7 @@
         break;
       }
     }
+    File = InputFile;
     return Args;
   }
 };
@@ -220,18 +224,22 @@
 
   bool Found = false;
   bool Ran = false;
-  Tool.appendArgumentsAdjuster(new CheckSyntaxOnlyAdjuster(Found, Ran));
+  std::string File;
+  Tool.appendArgumentsAdjuster(new CheckSyntaxOnlyAdjuster(Found, Ran, File));
   Tool.run(newFrontendActionFactory<SyntaxOnlyAction>());
   EXPECT_TRUE(Ran);
   EXPECT_TRUE(Found);
+  EXPECT_TRUE(File.compare(File.length() - 4, 4, "a.cc") == 0);
 
   Ran = Found = false;
+  File.clear();
   Tool.clearArgumentsAdjusters();
-  Tool.appendArgumentsAdjuster(new CheckSyntaxOnlyAdjuster(Found, Ran));
+  Tool.appendArgumentsAdjuster(new CheckSyntaxOnlyAdjuster(Found, Ran, File));
   Tool.appendArgumentsAdjuster(new ClangSyntaxOnlyAdjuster());
   Tool.run(newFrontendActionFactory<SyntaxOnlyAction>());
   EXPECT_TRUE(Ran);
   EXPECT_FALSE(Found);
+  EXPECT_TRUE(File.compare(File.length() - 4, 4, "a.cc") == 0);
 }
 
 } // end namespace tooling
Index: lib/Tooling/ArgumentsAdjusters.cpp
===================================================================
--- lib/Tooling/ArgumentsAdjusters.cpp  (revision 188102)
+++ lib/Tooling/ArgumentsAdjusters.cpp  (working copy)
@@ -24,7 +24,8 @@
 
 /// Add -fsyntax-only option to the commnand line arguments.
 CommandLineArguments
-ClangSyntaxOnlyAdjuster::Adjust(const CommandLineArguments &Args) {
+ClangSyntaxOnlyAdjuster::Adjust(StringRef InputFile,
+                                const CommandLineArguments &Args) {
   CommandLineArguments AdjustedArgs;
   for (size_t i = 0, e = Args.size(); i != e; ++i) {
     StringRef Arg = Args[i];
@@ -38,7 +39,8 @@
 }
 
 CommandLineArguments
-ClangStripOutputAdjuster::Adjust(const CommandLineArguments &Args) {
+ClangStripOutputAdjuster::Adjust(llvm::StringRef InputFile,
+                                 const CommandLineArguments &Args) {
   CommandLineArguments AdjustedArgs;
   for (size_t i = 0, e = Args.size(); i < e; ++i) {
     StringRef Arg = Args[i];
Index: lib/Tooling/Tooling.cpp
===================================================================
--- lib/Tooling/Tooling.cpp     (revision 188102)
+++ lib/Tooling/Tooling.cpp     (working copy)
@@ -308,7 +308,7 @@
                                CompileCommands[I].second.Directory + "\n!");
     std::vector<std::string> CommandLine = 
CompileCommands[I].second.CommandLine;
     for (unsigned I = 0, E = ArgsAdjusters.size(); I != E; ++I)
-      CommandLine = ArgsAdjusters[I]->Adjust(CommandLine);
+      CommandLine = ArgsAdjusters[I]->Adjust(File, CommandLine);
     assert(!CommandLine.empty());
     CommandLine[0] = MainExecutable;
     // FIXME: We need a callback mechanism for the tool writer to output a
Index: cpp11-migrate/Core/SyntaxCheck.cpp
===================================================================
--- cpp11-migrate/Core/SyntaxCheck.cpp  (revision 188102)
+++ cpp11-migrate/Core/SyntaxCheck.cpp  (working copy)
@@ -53,7 +53,8 @@
 };
 
 class SyntaxArgumentsAdjuster : public ArgumentsAdjuster {
-  CommandLineArguments Adjust(const CommandLineArguments &Args) {
+  CommandLineArguments Adjust(llvm::StringRef InputFile,
+                              const CommandLineArguments &Args) {
     CommandLineArguments AdjustedArgs = Args;
     AdjustedArgs.push_back("-fsyntax-only");
     AdjustedArgs.push_back("-std=c++11");
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to