blob: 5097c25832d068225061e127e230a77c68ee429e [file] [log] [blame]
Daniel Dunbar217acbf2009-11-19 07:37:51 +00001//===-- 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
Daniel Dunbar545c2812009-11-29 20:58:32 +000028int cc1_main(Diagnostic &Diags, const char **ArgBegin, const char **ArgEnd,
29 const char *Argv0, void *MainAddr) {
Daniel Dunbar217acbf2009-11-19 07:37:51 +000030 llvm::errs() << "cc1 argv:";
31 for (const char **i = ArgBegin; i != ArgEnd; ++i)
32 llvm::errs() << " \"" << *i << '"';
33 llvm::errs() << "\n";
34
35 // Parse the arguments.
36 OptTable *Opts = createCC1OptTable();
37 unsigned MissingArgIndex, MissingArgCount;
38 InputArgList *Args = Opts->ParseArgs(ArgBegin, ArgEnd,
39 MissingArgIndex, MissingArgCount);
40
41 // Check for missing argument error.
42 if (MissingArgCount)
43 Diags.Report(clang::diag::err_drv_missing_argument)
44 << Args->getArgString(MissingArgIndex) << MissingArgCount;
45
46 // Dump the parsed arguments.
47 llvm::errs() << "cc1 parsed options:\n";
48 for (ArgList::const_iterator it = Args->begin(), ie = Args->end();
49 it != ie; ++it)
50 (*it)->dump();
51
52 // Create a compiler invocation.
53 llvm::errs() << "cc1 creating invocation.\n";
54 CompilerInvocation Invocation;
Daniel Dunbar545c2812009-11-29 20:58:32 +000055 CompilerInvocation::CreateFromArgs(Invocation, ArgBegin, ArgEnd,
56 Argv0, MainAddr);
Daniel Dunbar217acbf2009-11-19 07:37:51 +000057
58 // Convert the invocation back to argument strings.
59 std::vector<std::string> InvocationArgs;
60 Invocation.toArgs(InvocationArgs);
61
62 // Dump the converted arguments.
Daniel Dunbar20be8c42009-11-20 22:47:55 +000063 llvm::SmallVector<const char*, 32> Invocation2Args;
Daniel Dunbar1be3b3b2009-11-19 20:54:59 +000064 llvm::errs() << "invocation argv :";
Daniel Dunbar217acbf2009-11-19 07:37:51 +000065 for (unsigned i = 0, e = InvocationArgs.size(); i != e; ++i) {
Daniel Dunbar20be8c42009-11-20 22:47:55 +000066 Invocation2Args.push_back(InvocationArgs[i].c_str());
Daniel Dunbar217acbf2009-11-19 07:37:51 +000067 llvm::errs() << " \"" << InvocationArgs[i] << '"';
68 }
69 llvm::errs() << "\n";
70
71 // Convert those arguments to another invocation, and check that we got the
72 // same thing.
73 CompilerInvocation Invocation2;
Daniel Dunbar20be8c42009-11-20 22:47:55 +000074 CompilerInvocation::CreateFromArgs(Invocation2, Invocation2Args.begin(),
Daniel Dunbar545c2812009-11-29 20:58:32 +000075 Invocation2Args.end(), Argv0, MainAddr);
Daniel Dunbar217acbf2009-11-19 07:37:51 +000076
77 // FIXME: Implement CompilerInvocation comparison.
Daniel Dunbar1be3b3b2009-11-19 20:54:59 +000078 if (true) {
79 //llvm::errs() << "warning: Invocations differ!\n";
Daniel Dunbar217acbf2009-11-19 07:37:51 +000080
81 std::vector<std::string> Invocation2Args;
82 Invocation2.toArgs(Invocation2Args);
Daniel Dunbar1be3b3b2009-11-19 20:54:59 +000083 llvm::errs() << "invocation2 argv:";
Daniel Dunbar217acbf2009-11-19 07:37:51 +000084 for (unsigned i = 0, e = Invocation2Args.size(); i != e; ++i)
85 llvm::errs() << " \"" << Invocation2Args[i] << '"';
86 llvm::errs() << "\n";
87 }
88
89 return 0;
90}