1 //===-- cc1_main.cpp - Clang CC1 Compiler Frontend ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
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/TargetSelect.h"
31 #include "llvm/Support/Timer.h"
32 #include "llvm/Support/raw_ostream.h"
33 #include "llvm/LinkAllPasses.h"
35 using namespace clang;
37 //===----------------------------------------------------------------------===//
39 //===----------------------------------------------------------------------===//
41 static void LLVMErrorHandler(void *UserData, const std::string &Message) {
42 DiagnosticsEngine &Diags = *static_cast<DiagnosticsEngine*>(UserData);
44 Diags.Report(diag::err_fe_error_backend) << Message;
46 // We cannot recover from llvm errors.
50 // FIXME: Define the need for this testing away.
51 static int cc1_test(DiagnosticsEngine &Diags,
52 const char **ArgBegin, const char **ArgEnd) {
53 using namespace clang::driver;
55 llvm::errs() << "cc1 argv:";
56 for (const char **i = ArgBegin; i != ArgEnd; ++i)
57 llvm::errs() << " \"" << *i << '"';
60 // Parse the arguments.
61 OptTable *Opts = createCC1OptTable();
62 unsigned MissingArgIndex, MissingArgCount;
63 InputArgList *Args = Opts->ParseArgs(ArgBegin, ArgEnd,
64 MissingArgIndex, MissingArgCount);
66 // Check for missing argument error.
68 Diags.Report(clang::diag::err_drv_missing_argument)
69 << Args->getArgString(MissingArgIndex) << MissingArgCount;
71 // Dump the parsed arguments.
72 llvm::errs() << "cc1 parsed options:\n";
73 for (ArgList::const_iterator it = Args->begin(), ie = Args->end();
77 // Create a compiler invocation.
78 llvm::errs() << "cc1 creating invocation.\n";
79 CompilerInvocation Invocation;
80 if (!CompilerInvocation::CreateFromArgs(Invocation, ArgBegin, ArgEnd, Diags))
83 // Convert the invocation back to argument strings.
84 std::vector<std::string> InvocationArgs;
85 Invocation.toArgs(InvocationArgs);
87 // Dump the converted arguments.
88 SmallVector<const char*, 32> Invocation2Args;
89 llvm::errs() << "invocation argv :";
90 for (unsigned i = 0, e = InvocationArgs.size(); i != e; ++i) {
91 Invocation2Args.push_back(InvocationArgs[i].c_str());
92 llvm::errs() << " \"" << InvocationArgs[i] << '"';
96 // Convert those arguments to another invocation, and check that we got the
98 CompilerInvocation Invocation2;
99 if (!CompilerInvocation::CreateFromArgs(Invocation2, Invocation2Args.begin(),
100 Invocation2Args.end(), Diags))
103 // FIXME: Implement CompilerInvocation comparison.
105 //llvm::errs() << "warning: Invocations differ!\n";
107 std::vector<std::string> Invocation2Args;
108 Invocation2.toArgs(Invocation2Args);
109 llvm::errs() << "invocation2 argv:";
110 for (unsigned i = 0, e = Invocation2Args.size(); i != e; ++i)
111 llvm::errs() << " \"" << Invocation2Args[i] << '"';
112 llvm::errs() << "\n";
118 int cc1_main(const char **ArgBegin, const char **ArgEnd,
119 const char *Argv0, void *MainAddr) {
120 OwningPtr<CompilerInstance> Clang(new CompilerInstance());
121 IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
123 // Run clang -cc1 test.
124 if (ArgBegin != ArgEnd && StringRef(ArgBegin[0]) == "-cc1test") {
125 DiagnosticsEngine Diags(DiagID, new TextDiagnosticPrinter(llvm::errs(),
126 DiagnosticOptions()));
127 return cc1_test(Diags, ArgBegin + 1, ArgEnd);
130 // Initialize targets first, so that --version shows registered targets.
131 llvm::InitializeAllTargets();
132 llvm::InitializeAllTargetMCs();
133 llvm::InitializeAllAsmPrinters();
134 llvm::InitializeAllAsmParsers();
136 // Buffer diagnostics from argument parsing so that we can output them using a
137 // well formed diagnostic object.
138 TextDiagnosticBuffer *DiagsBuffer = new TextDiagnosticBuffer;
139 DiagnosticsEngine Diags(DiagID, DiagsBuffer);
141 Success = CompilerInvocation::CreateFromArgs(Clang->getInvocation(),
142 ArgBegin, ArgEnd, Diags);
144 // Infer the builtin include path if unspecified.
145 if (Clang->getHeaderSearchOpts().UseBuiltinIncludes &&
146 Clang->getHeaderSearchOpts().ResourceDir.empty())
147 Clang->getHeaderSearchOpts().ResourceDir =
148 CompilerInvocation::GetResourcesPath(Argv0, MainAddr);
150 // Create the actual diagnostics engine.
151 Clang->createDiagnostics(ArgEnd - ArgBegin, const_cast<char**>(ArgBegin));
152 if (!Clang->hasDiagnostics())
155 // Set an error handler, so that any LLVM backend diagnostics go through our
157 llvm::install_fatal_error_handler(LLVMErrorHandler,
158 static_cast<void*>(&Clang->getDiagnostics()));
160 DiagsBuffer->FlushDiagnostics(Clang->getDiagnostics());
164 // Execute the frontend actions.
165 Success = ExecuteCompilerInvocation(Clang.get());
167 // If any timers were active but haven't been destroyed yet, print their
168 // results now. This happens in -disable-free mode.
169 llvm::TimerGroup::printAll(llvm::errs());
171 // Our error handler depends on the Diagnostics object, which we're
172 // potentially about to delete. Uninstall the handler now so that any
173 // later errors use the default handling behavior instead.
174 llvm::remove_fatal_error_handler();
176 // When running with -disable-free, don't do any destruction or shutdown.
177 if (Clang->getFrontendOpts().DisableFree) {
178 if (llvm::AreStatisticsEnabled() || Clang->getFrontendOpts().ShowStats)
179 llvm::PrintStatistics();
184 // Managed static deconstruction. Useful for making things like
185 // -time-passes usable.
186 llvm::llvm_shutdown();