cc1_main.cpp revision 20be8c4551d49fe66835baf1200be85ac2fd0c86
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
25using namespace clang;
26using namespace clang::driver;
27
28int 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, ArgBegin, ArgEnd);
55
56  // Convert the invocation back to argument strings.
57  std::vector<std::string> InvocationArgs;
58  Invocation.toArgs(InvocationArgs);
59
60  // Dump the converted arguments.
61  llvm::SmallVector<const char*, 32> Invocation2Args;
62  llvm::errs() << "invocation argv :";
63  for (unsigned i = 0, e = InvocationArgs.size(); i != e; ++i) {
64    Invocation2Args.push_back(InvocationArgs[i].c_str());
65    llvm::errs() << " \"" << InvocationArgs[i] << '"';
66  }
67  llvm::errs() << "\n";
68
69  // Convert those arguments to another invocation, and check that we got the
70  // same thing.
71  CompilerInvocation Invocation2;
72  CompilerInvocation::CreateFromArgs(Invocation2, Invocation2Args.begin(),
73                                     Invocation2Args.end());
74
75  // FIXME: Implement CompilerInvocation comparison.
76  if (true) {
77    //llvm::errs() << "warning: Invocations differ!\n";
78
79    std::vector<std::string> Invocation2Args;
80    Invocation2.toArgs(Invocation2Args);
81    llvm::errs() << "invocation2 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}
89