]> granicus.if.org Git - clang/commitdiff
Sketch some 'clang -cc1' support, for testing parts of CompilerInvocation.
authorDaniel Dunbar <daniel@zuster.org>
Thu, 19 Nov 2009 07:37:51 +0000 (07:37 +0000)
committerDaniel Dunbar <daniel@zuster.org>
Thu, 19 Nov 2009 07:37:51 +0000 (07:37 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@89333 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Frontend/CompilerInvocation.cpp
tools/driver/CMakeLists.txt
tools/driver/Makefile
tools/driver/cc1_main.cpp [new file with mode: 0644]
tools/driver/driver.cpp

index 10d8f3a3fa48dca10d2ab7518705e146d6306d8a..b4a79c6d1efb314f9dcc425239112bb8c6b564cb 100644 (file)
@@ -14,7 +14,6 @@ using namespace clang;
 
 void CompilerInvocation::CreateFromArgs(CompilerInvocation &Res,
                            const llvm::SmallVectorImpl<llvm::StringRef> &Args) {
-  llvm::llvm_report_error("FIXME: Not yet implemented!");
 }
 
 static const char *getAnalysisName(Analyses Kind) {
index 1123d0d54bc59a2f6cf24804c741b271b208526e..1d5d08b6c216bab46e57a72eb6dea0a27f087207 100644 (file)
@@ -3,12 +3,14 @@ set(LLVM_NO_RTTI 1)
 set( LLVM_USED_LIBS
   clangDriver
   clangBasic
+  clangFrontend
   )
 
 set(LLVM_LINK_COMPONENTS system support bitreader bitwriter)
 
 add_clang_executable(clang
   driver.cpp
+  cc1_main.cpp
   )
 
 add_dependencies(clang clang-cc)
index f250651ed7e258fb5a927fcef4299c185385a5de..19f93a29fa1c22515d211e1d81c9526d911ac4bd 100644 (file)
@@ -19,7 +19,7 @@ TOOL_NO_EXPORTS = 1
 # FIXME: It is unfortunate we need to pull in the bitcode reader and
 # writer just to get the serializer stuff used by clangBasic.
 LINK_COMPONENTS := system support bitreader bitwriter
-USEDLIBS = clangDriver.a clangBasic.a
+USEDLIBS = clangDriver.a clangBasic.a clangFrontend.a
 
 include $(LEVEL)/Makefile.common
 
diff --git a/tools/driver/cc1_main.cpp b/tools/driver/cc1_main.cpp
new file mode 100644 (file)
index 0000000..c516359
--- /dev/null
@@ -0,0 +1,88 @@
+//===-- cc1_main.cpp - Clang CC1 Driver -----------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This is the entry point to the clang -cc1 functionality.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/Driver/Arg.h"
+#include "clang/Driver/ArgList.h"
+#include "clang/Driver/CC1Options.h"
+#include "clang/Driver/DriverDiagnostic.h"
+#include "clang/Driver/OptTable.h"
+#include "clang/Driver/Option.h"
+#include "clang/Frontend/CompilerInvocation.h"
+#include "llvm/Support/raw_ostream.h"
+#include <cstdlib>
+#include <vector>
+
+using namespace clang;
+using namespace clang::driver;
+
+int cc1_main(Diagnostic &Diags, const char **ArgBegin, const char **ArgEnd) {
+  llvm::errs() << "cc1 argv:";
+  for (const char **i = ArgBegin; i != ArgEnd; ++i)
+    llvm::errs() << " \"" << *i << '"';
+  llvm::errs() << "\n";
+
+  // Parse the arguments.
+  OptTable *Opts = createCC1OptTable();
+  unsigned MissingArgIndex, MissingArgCount;
+  InputArgList *Args = Opts->ParseArgs(ArgBegin, ArgEnd,
+                                       MissingArgIndex, MissingArgCount);
+
+  // Check for missing argument error.
+  if (MissingArgCount)
+    Diags.Report(clang::diag::err_drv_missing_argument)
+      << Args->getArgString(MissingArgIndex) << MissingArgCount;
+
+  // Dump the parsed arguments.
+  llvm::errs() << "cc1 parsed options:\n";
+  for (ArgList::const_iterator it = Args->begin(), ie = Args->end();
+       it != ie; ++it)
+    (*it)->dump();
+
+  // Create a compiler invocation.
+  llvm::errs() << "cc1 creating invocation.\n";
+  CompilerInvocation Invocation;
+  CompilerInvocation::CreateFromArgs(Invocation,
+                      llvm::SmallVector<llvm::StringRef, 32>(ArgBegin, ArgEnd));
+
+  // Convert the invocation back to argument strings.
+  std::vector<std::string> InvocationArgs;
+  Invocation.toArgs(InvocationArgs);
+
+  // Dump the converted arguments.
+  llvm::SmallVector<llvm::StringRef, 32> Invocation2Args;
+  llvm::errs() << "invocation argv:";
+  for (unsigned i = 0, e = InvocationArgs.size(); i != e; ++i) {
+    Invocation2Args.push_back(InvocationArgs[i]);
+    llvm::errs() << " \"" << InvocationArgs[i] << '"';
+  }
+  llvm::errs() << "\n";
+
+  // Convert those arguments to another invocation, and check that we got the
+  // same thing.
+  CompilerInvocation Invocation2;
+  CompilerInvocation::CreateFromArgs(Invocation2, Invocation2Args);
+
+  // FIXME: Implement CompilerInvocation comparison.
+  if (memcmp(&Invocation, &Invocation2, sizeof(Invocation)) != 0) {
+    llvm::errs() << "warning: Invocations differ!\n";
+
+    std::vector<std::string> Invocation2Args;
+    Invocation2.toArgs(Invocation2Args);
+    llvm::errs() << "invocation argv:";
+    for (unsigned i = 0, e = Invocation2Args.size(); i != e; ++i)
+      llvm::errs() << " \"" << Invocation2Args[i] << '"';
+    llvm::errs() << "\n";
+  }
+
+  return 0;
+}
index 43dd36963c6d05e6178384d657b8dd11a39797be..dbfc29365561f39b5c982cdc2392d00e717fbc05 100644 (file)
@@ -178,6 +178,9 @@ void ApplyQAOverride(std::vector<const char*> &Args, const char *OverrideStr,
   }
 }
 
+extern int cc1_main(Diagnostic &Diags,
+                    const char **ArgBegin, const char **ArgEnd);
+
 int main(int argc, const char **argv) {
   llvm::sys::PrintStackTraceOnErrorSignal();
   llvm::PrettyStackTraceProgram X(argc, argv);
@@ -187,6 +190,10 @@ int main(int argc, const char **argv) {
 
   Diagnostic Diags(&DiagClient);
 
+  // Dispatch to cc1_main if appropriate.
+  if (argc > 1 && llvm::StringRef(argv[1]) == "-cc1")
+    return cc1_main(Diags, argv+2, argv+argc);
+
 #ifdef CLANG_IS_PRODUCTION
   bool IsProduction = true;
 #else