xref: /llvm-project/clang/lib/Frontend/CreateInvocationFromCommandLine.cpp (revision d0de8ceb577fdad9ef6873914b665061b6852a2c)
1 //===--- CreateInvocationFromCommandLine.cpp - CompilerInvocation from Args ==//
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 // Construct a compiler invocation object for command line driver arguments
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/Basic/DiagnosticOptions.h"
15 #include "clang/Frontend/Utils.h"
16 #include "clang/Frontend/CompilerInstance.h"
17 #include "clang/Frontend/FrontendDiagnostic.h"
18 #include "clang/Driver/Compilation.h"
19 #include "clang/Driver/Driver.h"
20 #include "clang/Driver/ArgList.h"
21 #include "clang/Driver/Options.h"
22 #include "clang/Driver/Tool.h"
23 #include "llvm/Support/Host.h"
24 using namespace clang;
25 
26 /// createInvocationFromCommandLine - Construct a compiler invocation object for
27 /// a command line argument vector.
28 ///
29 /// \return A CompilerInvocation, or 0 if none was built for the given
30 /// argument vector.
31 CompilerInvocation *
32 clang::createInvocationFromCommandLine(ArrayRef<const char *> ArgList,
33                             IntrusiveRefCntPtr<DiagnosticsEngine> Diags) {
34   if (!Diags.getPtr()) {
35     // No diagnostics engine was provided, so create our own diagnostics object
36     // with the default options.
37     Diags = CompilerInstance::createDiagnostics(new DiagnosticOptions,
38                                                 ArgList.size(),
39                                                 ArgList.begin());
40   }
41 
42   SmallVector<const char *, 16> Args;
43   Args.push_back("<clang>"); // FIXME: Remove dummy argument.
44   Args.insert(Args.end(), ArgList.begin(), ArgList.end());
45 
46   // FIXME: Find a cleaner way to force the driver into restricted modes.
47   Args.push_back("-fsyntax-only");
48 
49   // FIXME: We shouldn't have to pass in the path info.
50   driver::Driver TheDriver("clang", llvm::sys::getDefaultTargetTriple(),
51                            "a.out", false, *Diags);
52 
53   // Don't check that inputs exist, they may have been remapped.
54   TheDriver.setCheckInputsExist(false);
55 
56   OwningPtr<driver::Compilation> C(TheDriver.BuildCompilation(Args));
57 
58   // Just print the cc1 options if -### was present.
59   if (C->getArgs().hasArg(driver::options::OPT__HASH_HASH_HASH)) {
60     C->PrintJob(llvm::errs(), C->getJobs(), "\n", true);
61     return 0;
62   }
63 
64   // We expect to get back exactly one command job, if we didn't something
65   // failed.
66   const driver::JobList &Jobs = C->getJobs();
67   if (Jobs.size() != 1 || !isa<driver::Command>(*Jobs.begin())) {
68     SmallString<256> Msg;
69     llvm::raw_svector_ostream OS(Msg);
70     C->PrintJob(OS, C->getJobs(), "; ", true);
71     Diags->Report(diag::err_fe_expected_compiler_job) << OS.str();
72     return 0;
73   }
74 
75   const driver::Command *Cmd = cast<driver::Command>(*Jobs.begin());
76   if (StringRef(Cmd->getCreator().getName()) != "clang") {
77     Diags->Report(diag::err_fe_expected_clang_command);
78     return 0;
79   }
80 
81   const driver::ArgStringList &CCArgs = Cmd->getArguments();
82   OwningPtr<CompilerInvocation> CI(new CompilerInvocation());
83   if (!CompilerInvocation::CreateFromArgs(*CI,
84                                      const_cast<const char **>(CCArgs.data()),
85                                      const_cast<const char **>(CCArgs.data()) +
86                                      CCArgs.size(),
87                                      *Diags))
88     return 0;
89   return CI.take();
90 }
91