1 //===-- cc1_main.cpp - Clang CC1 Compiler Frontend ------------------------===//
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, which implements the
11 // core compiler functionality along with a number of additional tools for
12 // demonstration and testing purposes.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "clang/Driver/Arg.h"
17 #include "clang/Driver/ArgList.h"
18 #include "clang/Driver/CC1Options.h"
19 #include "clang/Driver/DriverDiagnostic.h"
20 #include "clang/Driver/OptTable.h"
21 #include "clang/Frontend/CompilerInstance.h"
22 #include "clang/Frontend/CompilerInvocation.h"
23 #include "clang/Frontend/FrontendDiagnostic.h"
24 #include "clang/Frontend/TextDiagnosticBuffer.h"
25 #include "clang/Frontend/TextDiagnosticPrinter.h"
26 #include "clang/FrontendTool/Utils.h"
27 #include "llvm/ADT/Statistic.h"
28 #include "llvm/Support/ErrorHandling.h"
29 #include "llvm/Support/ManagedStatic.h"
30 #include "llvm/Support/Timer.h"
31 #include "llvm/Support/raw_ostream.h"
32 #include "llvm/Target/TargetSelect.h"
33 #include <cstdio>
34 using namespace clang;
35
36 //===----------------------------------------------------------------------===//
37 // Main driver
38 //===----------------------------------------------------------------------===//
39
LLVMErrorHandler(void * UserData,const std::string & Message)40 static void LLVMErrorHandler(void *UserData, const std::string &Message) {
41 Diagnostic &Diags = *static_cast<Diagnostic*>(UserData);
42
43 Diags.Report(diag::err_fe_error_backend) << Message;
44
45 // We cannot recover from llvm errors.
46 exit(1);
47 }
48
49 // FIXME: Define the need for this testing away.
cc1_test(Diagnostic & Diags,const char ** ArgBegin,const char ** ArgEnd)50 static int cc1_test(Diagnostic &Diags,
51 const char **ArgBegin, const char **ArgEnd) {
52 using namespace clang::driver;
53
54 llvm::errs() << "cc1 argv:";
55 for (const char **i = ArgBegin; i != ArgEnd; ++i)
56 llvm::errs() << " \"" << *i << '"';
57 llvm::errs() << "\n";
58
59 // Parse the arguments.
60 OptTable *Opts = createCC1OptTable();
61 unsigned MissingArgIndex, MissingArgCount;
62 InputArgList *Args = Opts->ParseArgs(ArgBegin, ArgEnd,
63 MissingArgIndex, MissingArgCount);
64
65 // Check for missing argument error.
66 if (MissingArgCount)
67 Diags.Report(clang::diag::err_drv_missing_argument)
68 << Args->getArgString(MissingArgIndex) << MissingArgCount;
69
70 // Dump the parsed arguments.
71 llvm::errs() << "cc1 parsed options:\n";
72 for (ArgList::const_iterator it = Args->begin(), ie = Args->end();
73 it != ie; ++it)
74 (*it)->dump();
75
76 // Create a compiler invocation.
77 llvm::errs() << "cc1 creating invocation.\n";
78 CompilerInvocation Invocation;
79 CompilerInvocation::CreateFromArgs(Invocation, ArgBegin, ArgEnd, Diags);
80
81 // Convert the invocation back to argument strings.
82 std::vector<std::string> InvocationArgs;
83 Invocation.toArgs(InvocationArgs);
84
85 // Dump the converted arguments.
86 llvm::SmallVector<const char*, 32> Invocation2Args;
87 llvm::errs() << "invocation argv :";
88 for (unsigned i = 0, e = InvocationArgs.size(); i != e; ++i) {
89 Invocation2Args.push_back(InvocationArgs[i].c_str());
90 llvm::errs() << " \"" << InvocationArgs[i] << '"';
91 }
92 llvm::errs() << "\n";
93
94 // Convert those arguments to another invocation, and check that we got the
95 // same thing.
96 CompilerInvocation Invocation2;
97 CompilerInvocation::CreateFromArgs(Invocation2, Invocation2Args.begin(),
98 Invocation2Args.end(), Diags);
99
100 // FIXME: Implement CompilerInvocation comparison.
101 if (true) {
102 //llvm::errs() << "warning: Invocations differ!\n";
103
104 std::vector<std::string> Invocation2Args;
105 Invocation2.toArgs(Invocation2Args);
106 llvm::errs() << "invocation2 argv:";
107 for (unsigned i = 0, e = Invocation2Args.size(); i != e; ++i)
108 llvm::errs() << " \"" << Invocation2Args[i] << '"';
109 llvm::errs() << "\n";
110 }
111
112 return 0;
113 }
114
cc1_main(const char ** ArgBegin,const char ** ArgEnd,const char * Argv0,void * MainAddr)115 int cc1_main(const char **ArgBegin, const char **ArgEnd,
116 const char *Argv0, void *MainAddr) {
117 llvm::OwningPtr<CompilerInstance> Clang(new CompilerInstance());
118 llvm::IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
119
120 // Run clang -cc1 test.
121 if (ArgBegin != ArgEnd && llvm::StringRef(ArgBegin[0]) == "-cc1test") {
122 Diagnostic Diags(DiagID, new TextDiagnosticPrinter(llvm::errs(),
123 DiagnosticOptions()));
124 return cc1_test(Diags, ArgBegin + 1, ArgEnd);
125 }
126
127 // Initialize targets first, so that --version shows registered targets.
128 llvm::InitializeAllTargets();
129 llvm::InitializeAllMCAsmInfos();
130 llvm::InitializeAllMCCodeGenInfos();
131 llvm::InitializeAllMCSubtargetInfos();
132 llvm::InitializeAllAsmPrinters();
133 llvm::InitializeAllAsmParsers();
134
135 // Buffer diagnostics from argument parsing so that we can output them using a
136 // well formed diagnostic object.
137 TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer;
138 Diagnostic Diags(DiagID, DiagsBuffer);
139 CompilerInvocation::CreateFromArgs(Clang->getInvocation(), ArgBegin, ArgEnd,
140 Diags);
141
142 // Infer the builtin include path if unspecified.
143 if (Clang->getHeaderSearchOpts().UseBuiltinIncludes &&
144 Clang->getHeaderSearchOpts().ResourceDir.empty())
145 Clang->getHeaderSearchOpts().ResourceDir =
146 CompilerInvocation::GetResourcesPath(Argv0, MainAddr);
147
148 // Create the actual diagnostics engine.
149 Clang->createDiagnostics(ArgEnd - ArgBegin, const_cast<char**>(ArgBegin));
150 if (!Clang->hasDiagnostics())
151 return 1;
152
153 // Set an error handler, so that any LLVM backend diagnostics go through our
154 // error handler.
155 llvm::install_fatal_error_handler(LLVMErrorHandler,
156 static_cast<void*>(&Clang->getDiagnostics()));
157
158 DiagsBuffer->FlushDiagnostics(Clang->getDiagnostics());
159
160 // Execute the frontend actions.
161 bool Success = ExecuteCompilerInvocation(Clang.get());
162
163 // If any timers were active but haven't been destroyed yet, print their
164 // results now. This happens in -disable-free mode.
165 llvm::TimerGroup::printAll(llvm::errs());
166
167 // Our error handler depends on the Diagnostics object, which we're
168 // potentially about to delete. Uninstall the handler now so that any
169 // later errors use the default handling behavior instead.
170 llvm::remove_fatal_error_handler();
171
172 // When running with -disable-free, don't do any destruction or shutdown.
173 if (Clang->getFrontendOpts().DisableFree) {
174 if (llvm::AreStatisticsEnabled() || Clang->getFrontendOpts().ShowStats)
175 llvm::PrintStatistics();
176 Clang.take();
177 return !Success;
178 }
179
180 // Managed static deconstruction. Useful for making things like
181 // -time-passes usable.
182 llvm::llvm_shutdown();
183
184 return !Success;
185 }
186