diff options
author | dim <dim@FreeBSD.org> | 2014-03-21 17:53:59 +0000 |
---|---|---|
committer | dim <dim@FreeBSD.org> | 2014-03-21 17:53:59 +0000 |
commit | 9cedb8bb69b89b0f0c529937247a6a80cabdbaec (patch) | |
tree | c978f0e9ec1ab92dc8123783f30b08a7fd1e2a39 /contrib/llvm/tools/clang/lib/Tooling/CompilationDatabase.cpp | |
parent | 03fdc2934eb61c44c049a02b02aa974cfdd8a0eb (diff) | |
download | FreeBSD-src-9cedb8bb69b89b0f0c529937247a6a80cabdbaec.zip FreeBSD-src-9cedb8bb69b89b0f0c529937247a6a80cabdbaec.tar.gz |
MFC 261991:
Upgrade our copy of llvm/clang to 3.4 release. This version supports
all of the features in the current working draft of the upcoming C++
standard, provisionally named C++1y.
The code generator's performance is greatly increased, and the loop
auto-vectorizer is now enabled at -Os and -O2 in addition to -O3. The
PowerPC backend has made several major improvements to code generation
quality and compile time, and the X86, SPARC, ARM32, Aarch64 and SystemZ
backends have all seen major feature work.
Release notes for llvm and clang can be found here:
<http://llvm.org/releases/3.4/docs/ReleaseNotes.html>
<http://llvm.org/releases/3.4/tools/clang/docs/ReleaseNotes.html>
MFC 262121 (by emaste):
Update lldb for clang/llvm 3.4 import
This commit largely restores the lldb source to the upstream r196259
snapshot with the addition of threaded inferior support and a few bug
fixes.
Specific upstream lldb revisions restored include:
SVN git
181387 779e6ac
181703 7bef4e2
182099 b31044e
182650 f2dcf35
182683 0d91b80
183862 15c1774
183929 99447a6
184177 0b2934b
184948 4dc3761
184954 007e7bc
186990 eebd175
Sponsored by: DARPA, AFRL
MFC 262186 (by emaste):
Fix mismerge in r262121
A break statement was lost in the merge. The error had no functional
impact, but restore it to reduce the diff against upstream.
MFC 262303:
Pull in r197521 from upstream clang trunk (by rdivacky):
Use the integrated assembler by default on FreeBSD/ppc and ppc64.
Requested by: jhibbits
MFC 262611:
Pull in r196874 from upstream llvm trunk:
Fix a crash that occurs when PWD is invalid.
MCJIT needs to be able to run in hostile environments, even when PWD
is invalid. There's no need to crash MCJIT in this case.
The obvious fix is to simply leave MCContext's CompilationDir empty
when PWD can't be determined. This way, MCJIT clients,
and other clients that link with LLVM don't need a valid working directory.
If we do want to guarantee valid CompilationDir, that should be done
only for clients of getCompilationDir(). This is as simple as checking
for an empty string.
The only current use of getCompilationDir is EmitGenDwarfInfo, which
won't conceivably run with an invalid working dir. However, in the
purely hypothetically and untestable case that this happens, the
AT_comp_dir will be omitted from the compilation_unit DIE.
This should help fix assertions occurring with ports-mgmt/tinderbox,
when it is using jails, and sometimes invalidates clang's current
working directory.
Reported by: decke
MFC 262809:
Pull in r203007 from upstream clang trunk:
Don't produce an alias between destructors with different calling conventions.
Fixes pr19007.
(Please note that is an LLVM PR identifier, not a FreeBSD one.)
This should fix Firefox and/or libxul crashes (due to problems with
regparm/stdcall calling conventions) on i386.
Reported by: multiple users on freebsd-current
PR: bin/187103
MFC 263048:
Repair recognition of "CC" as an alias for the C++ compiler, since it
was silently broken by upstream for a Windows-specific use-case.
Apparently some versions of CMake still rely on this archaic feature...
Reported by: rakuco
MFC 263049:
Garbage collect the old way of adding the libstdc++ include directories
in clang's InitHeaderSearch.cpp. This has been superseded by David
Chisnall's commit in r255321.
Moreover, if libc++ is used, the libstdc++ include directories should
not be in the search path at all. These directories are now only used
if you pass -stdlib=libstdc++.
Diffstat (limited to 'contrib/llvm/tools/clang/lib/Tooling/CompilationDatabase.cpp')
-rw-r--r-- | contrib/llvm/tools/clang/lib/Tooling/CompilationDatabase.cpp | 185 |
1 files changed, 183 insertions, 2 deletions
diff --git a/contrib/llvm/tools/clang/lib/Tooling/CompilationDatabase.cpp b/contrib/llvm/tools/clang/lib/Tooling/CompilationDatabase.cpp index b5b99cb..c962055 100644 --- a/contrib/llvm/tools/clang/lib/Tooling/CompilationDatabase.cpp +++ b/contrib/llvm/tools/clang/lib/Tooling/CompilationDatabase.cpp @@ -20,6 +20,16 @@ #include "llvm/Support/system_error.h" #include <sstream> +#include "clang/Basic/Diagnostic.h" +#include "clang/Driver/Action.h" +#include "clang/Driver/Driver.h" +#include "clang/Driver/DriverDiagnostic.h" +#include "clang/Driver/Job.h" +#include "clang/Driver/Compilation.h" +#include "clang/Frontend/TextDiagnosticPrinter.h" +#include "llvm/Support/Host.h" +#include "llvm/Option/Arg.h" + namespace clang { namespace tooling { @@ -100,6 +110,173 @@ CompilationDatabase::autoDetectFromDirectory(StringRef SourceDir, CompilationDatabasePlugin::~CompilationDatabasePlugin() {} +// Helper for recursively searching through a chain of actions and collecting +// all inputs, direct and indirect, of compile jobs. +struct CompileJobAnalyzer { + void run(const driver::Action *A) { + runImpl(A, false); + } + + SmallVector<std::string, 2> Inputs; + +private: + + void runImpl(const driver::Action *A, bool Collect) { + bool CollectChildren = Collect; + switch (A->getKind()) { + case driver::Action::CompileJobClass: + CollectChildren = true; + break; + + case driver::Action::InputClass: { + if (Collect) { + const driver::InputAction *IA = cast<driver::InputAction>(A); + Inputs.push_back(IA->getInputArg().getSpelling()); + } + } break; + + default: + // Don't care about others + ; + } + + for (driver::ActionList::const_iterator I = A->begin(), E = A->end(); + I != E; ++I) + runImpl(*I, CollectChildren); + } +}; + +// Special DiagnosticConsumer that looks for warn_drv_input_file_unused +// diagnostics from the driver and collects the option strings for those unused +// options. +class UnusedInputDiagConsumer : public DiagnosticConsumer { +public: + UnusedInputDiagConsumer() : Other(0) {} + + // Useful for debugging, chain diagnostics to another consumer after + // recording for our own purposes. + UnusedInputDiagConsumer(DiagnosticConsumer *Other) : Other(Other) {} + + virtual void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, + const Diagnostic &Info) LLVM_OVERRIDE { + if (Info.getID() == clang::diag::warn_drv_input_file_unused) { + // Arg 1 for this diagnostic is the option that didn't get used. + UnusedInputs.push_back(Info.getArgStdStr(0)); + } + if (Other) + Other->HandleDiagnostic(DiagLevel, Info); + } + + DiagnosticConsumer *Other; + SmallVector<std::string, 2> UnusedInputs; +}; + +// Unary functor for asking "Given a StringRef S1, does there exist a string +// S2 in Arr where S1 == S2?" +struct MatchesAny { + MatchesAny(ArrayRef<std::string> Arr) : Arr(Arr) {} + bool operator() (StringRef S) { + for (const std::string *I = Arr.begin(), *E = Arr.end(); I != E; ++I) + if (*I == S) + return true; + return false; + } +private: + ArrayRef<std::string> Arr; +}; + +/// \brief Strips any positional args and possible argv[0] from a command-line +/// provided by the user to construct a FixedCompilationDatabase. +/// +/// FixedCompilationDatabase requires a command line to be in this format as it +/// constructs the command line for each file by appending the name of the file +/// to be compiled. FixedCompilationDatabase also adds its own argv[0] to the +/// start of the command line although its value is not important as it's just +/// ignored by the Driver invoked by the ClangTool using the +/// FixedCompilationDatabase. +/// +/// FIXME: This functionality should probably be made available by +/// clang::driver::Driver although what the interface should look like is not +/// clear. +/// +/// \param[in] Args Args as provided by the user. +/// \return Resulting stripped command line. +/// \li true if successful. +/// \li false if \c Args cannot be used for compilation jobs (e.g. +/// contains an option like -E or -version). +bool stripPositionalArgs(std::vector<const char *> Args, + std::vector<std::string> &Result) { + IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); + UnusedInputDiagConsumer DiagClient; + DiagnosticsEngine Diagnostics( + IntrusiveRefCntPtr<clang::DiagnosticIDs>(new DiagnosticIDs()), + &*DiagOpts, &DiagClient, false); + + // Neither clang executable nor default image name are required since the + // jobs the driver builds will not be executed. + OwningPtr<driver::Driver> NewDriver(new driver::Driver( + /* ClangExecutable= */ "", llvm::sys::getDefaultTargetTriple(), + /* DefaultImageName= */ "", Diagnostics)); + NewDriver->setCheckInputsExist(false); + + // This becomes the new argv[0]. The value is actually not important as it + // isn't used for invoking Tools. + Args.insert(Args.begin(), "clang-tool"); + + // By adding -c, we force the driver to treat compilation as the last phase. + // It will then issue warnings via Diagnostics about un-used options that + // would have been used for linking. If the user provided a compiler name as + // the original argv[0], this will be treated as a linker input thanks to + // insertng a new argv[0] above. All un-used options get collected by + // UnusedInputdiagConsumer and get stripped out later. + Args.push_back("-c"); + + // Put a dummy C++ file on to ensure there's at least one compile job for the + // driver to construct. If the user specified some other argument that + // prevents compilation, e.g. -E or something like -version, we may still end + // up with no jobs but then this is the user's fault. + Args.push_back("placeholder.cpp"); + + const OwningPtr<driver::Compilation> Compilation( + NewDriver->BuildCompilation(Args)); + + const driver::JobList &Jobs = Compilation->getJobs(); + + CompileJobAnalyzer CompileAnalyzer; + + for (driver::JobList::const_iterator I = Jobs.begin(), E = Jobs.end(); I != E; + ++I) { + if ((*I)->getKind() == driver::Job::CommandClass) { + const driver::Command *Cmd = cast<driver::Command>(*I); + // Collect only for Assemble jobs. If we do all jobs we get duplicates + // since Link jobs point to Assemble jobs as inputs. + if (Cmd->getSource().getKind() == driver::Action::AssembleJobClass) + CompileAnalyzer.run(&Cmd->getSource()); + } + } + + if (CompileAnalyzer.Inputs.empty()) { + // No compile jobs found. + // FIXME: Emit a warning of some kind? + return false; + } + + // Remove all compilation input files from the command line. This is + // necessary so that getCompileCommands() can construct a command line for + // each file. + std::vector<const char *>::iterator End = std::remove_if( + Args.begin(), Args.end(), MatchesAny(CompileAnalyzer.Inputs)); + + // Remove all inputs deemed unused for compilation. + End = std::remove_if(Args.begin(), End, MatchesAny(DiagClient.UnusedInputs)); + + // Remove the -c add above as well. It will be at the end right now. + --End; + + Result = std::vector<std::string>(Args.begin() + 1, End); + return true; +} + FixedCompilationDatabase * FixedCompilationDatabase::loadFromCommandLine(int &Argc, const char **Argv, @@ -107,9 +284,13 @@ FixedCompilationDatabase::loadFromCommandLine(int &Argc, const char **DoubleDash = std::find(Argv, Argv + Argc, StringRef("--")); if (DoubleDash == Argv + Argc) return NULL; - std::vector<std::string> CommandLine(DoubleDash + 1, Argv + Argc); + std::vector<const char *> CommandLine(DoubleDash + 1, Argv + Argc); Argc = DoubleDash - Argv; - return new FixedCompilationDatabase(Directory, CommandLine); + + std::vector<std::string> StrippedArgs; + if (!stripPositionalArgs(CommandLine, StrippedArgs)) + return 0; + return new FixedCompilationDatabase(Directory, StrippedArgs); } FixedCompilationDatabase:: |