Daniel Dunbar | 217acbf | 2009-11-19 07:37:51 +0000 | [diff] [blame^] | 1 | //===-- cc1_main.cpp - Clang CC1 Driver -----------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This is the entry point to the clang -cc1 functionality. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Driver/Arg.h" |
| 15 | #include "clang/Driver/ArgList.h" |
| 16 | #include "clang/Driver/CC1Options.h" |
| 17 | #include "clang/Driver/DriverDiagnostic.h" |
| 18 | #include "clang/Driver/OptTable.h" |
| 19 | #include "clang/Driver/Option.h" |
| 20 | #include "clang/Frontend/CompilerInvocation.h" |
| 21 | #include "llvm/Support/raw_ostream.h" |
| 22 | #include <cstdlib> |
| 23 | #include <vector> |
| 24 | |
| 25 | using namespace clang; |
| 26 | using namespace clang::driver; |
| 27 | |
| 28 | int cc1_main(Diagnostic &Diags, const char **ArgBegin, const char **ArgEnd) { |
| 29 | llvm::errs() << "cc1 argv:"; |
| 30 | for (const char **i = ArgBegin; i != ArgEnd; ++i) |
| 31 | llvm::errs() << " \"" << *i << '"'; |
| 32 | llvm::errs() << "\n"; |
| 33 | |
| 34 | // Parse the arguments. |
| 35 | OptTable *Opts = createCC1OptTable(); |
| 36 | unsigned MissingArgIndex, MissingArgCount; |
| 37 | InputArgList *Args = Opts->ParseArgs(ArgBegin, ArgEnd, |
| 38 | MissingArgIndex, MissingArgCount); |
| 39 | |
| 40 | // Check for missing argument error. |
| 41 | if (MissingArgCount) |
| 42 | Diags.Report(clang::diag::err_drv_missing_argument) |
| 43 | << Args->getArgString(MissingArgIndex) << MissingArgCount; |
| 44 | |
| 45 | // Dump the parsed arguments. |
| 46 | llvm::errs() << "cc1 parsed options:\n"; |
| 47 | for (ArgList::const_iterator it = Args->begin(), ie = Args->end(); |
| 48 | it != ie; ++it) |
| 49 | (*it)->dump(); |
| 50 | |
| 51 | // Create a compiler invocation. |
| 52 | llvm::errs() << "cc1 creating invocation.\n"; |
| 53 | CompilerInvocation Invocation; |
| 54 | CompilerInvocation::CreateFromArgs(Invocation, |
| 55 | llvm::SmallVector<llvm::StringRef, 32>(ArgBegin, ArgEnd)); |
| 56 | |
| 57 | // Convert the invocation back to argument strings. |
| 58 | std::vector<std::string> InvocationArgs; |
| 59 | Invocation.toArgs(InvocationArgs); |
| 60 | |
| 61 | // Dump the converted arguments. |
| 62 | llvm::SmallVector<llvm::StringRef, 32> Invocation2Args; |
| 63 | llvm::errs() << "invocation argv:"; |
| 64 | for (unsigned i = 0, e = InvocationArgs.size(); i != e; ++i) { |
| 65 | Invocation2Args.push_back(InvocationArgs[i]); |
| 66 | llvm::errs() << " \"" << InvocationArgs[i] << '"'; |
| 67 | } |
| 68 | llvm::errs() << "\n"; |
| 69 | |
| 70 | // Convert those arguments to another invocation, and check that we got the |
| 71 | // same thing. |
| 72 | CompilerInvocation Invocation2; |
| 73 | CompilerInvocation::CreateFromArgs(Invocation2, Invocation2Args); |
| 74 | |
| 75 | // FIXME: Implement CompilerInvocation comparison. |
| 76 | if (memcmp(&Invocation, &Invocation2, sizeof(Invocation)) != 0) { |
| 77 | llvm::errs() << "warning: Invocations differ!\n"; |
| 78 | |
| 79 | std::vector<std::string> Invocation2Args; |
| 80 | Invocation2.toArgs(Invocation2Args); |
| 81 | llvm::errs() << "invocation argv:"; |
| 82 | for (unsigned i = 0, e = Invocation2Args.size(); i != e; ++i) |
| 83 | llvm::errs() << " \"" << Invocation2Args[i] << '"'; |
| 84 | llvm::errs() << "\n"; |
| 85 | } |
| 86 | |
| 87 | return 0; |
| 88 | } |