diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2019-08-20 20:50:49 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2019-08-20 20:50:49 +0000 |
commit | 2298981669bf3bd63335a4be179bc0f96823a8f4 (patch) | |
tree | 1cbe2eb27f030d2d70b80ee5ca3c86bee7326a9f /tools | |
parent | 9a83721404652cea39e9f02ae3e3b5c964602a5c (diff) |
Notes
Diffstat (limited to 'tools')
-rw-r--r-- | tools/clang-format/ClangFormat.cpp | 39 | ||||
-rw-r--r-- | tools/driver/cc1_main.cpp | 62 | ||||
-rw-r--r-- | tools/driver/cc1as_main.cpp | 43 | ||||
-rw-r--r-- | tools/driver/cc1gen_reproducer_main.cpp | 7 | ||||
-rw-r--r-- | tools/driver/driver.cpp | 9 |
5 files changed, 126 insertions, 34 deletions
diff --git a/tools/clang-format/ClangFormat.cpp b/tools/clang-format/ClangFormat.cpp index 49162d0d910e5..60a7feb7119b4 100644 --- a/tools/clang-format/ClangFormat.cpp +++ b/tools/clang-format/ClangFormat.cpp @@ -1,9 +1,8 @@ //===-- clang-format/ClangFormat.cpp - Clang format tool ------------------===// // -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// /// @@ -258,6 +257,36 @@ static bool format(StringRef FileName) { std::unique_ptr<llvm::MemoryBuffer> Code = std::move(CodeOrErr.get()); if (Code->getBufferSize() == 0) return false; // Empty files are formatted correctly. + + // Check to see if the buffer has a UTF Byte Order Mark (BOM). + // We only support UTF-8 with and without a BOM right now. See + // https://en.wikipedia.org/wiki/Byte_order_mark#Byte_order_marks_by_encoding + // for more information. + StringRef BufStr = Code->getBuffer(); + const char *InvalidBOM = llvm::StringSwitch<const char *>(BufStr) + .StartsWith(llvm::StringLiteral::withInnerNUL("\x00\x00\xFE\xFF"), + "UTF-32 (BE)") + .StartsWith(llvm::StringLiteral::withInnerNUL("\xFF\xFE\x00\x00"), + "UTF-32 (LE)") + .StartsWith("\xFE\xFF", "UTF-16 (BE)") + .StartsWith("\xFF\xFE", "UTF-16 (LE)") + .StartsWith("\x2B\x2F\x76", "UTF-7") + .StartsWith("\xF7\x64\x4C", "UTF-1") + .StartsWith("\xDD\x73\x66\x73", "UTF-EBCDIC") + .StartsWith("\x0E\xFE\xFF", "SCSU") + .StartsWith("\xFB\xEE\x28", "BOCU-1") + .StartsWith("\x84\x31\x95\x33", "GB-18030") + .Default(nullptr); + + if (InvalidBOM) { + errs() << "error: encoding with unsupported byte order mark \"" + << InvalidBOM << "\" detected"; + if (FileName != "-") + errs() << " in file '" << FileName << "'"; + errs() << ".\n"; + return true; + } + std::vector<tooling::Range> Ranges; if (fillRanges(Code.get(), Ranges)) return true; @@ -346,7 +375,7 @@ int main(int argc, const char **argv) { cl::SetVersionPrinter(PrintVersion); cl::ParseCommandLineOptions( argc, argv, - "A tool to format C/C++/Java/JavaScript/Objective-C/Protobuf code.\n\n" + "A tool to format C/C++/Java/JavaScript/Objective-C/Protobuf/C# code.\n\n" "If no arguments are specified, it formats the code from standard input\n" "and writes the result to the standard output.\n" "If <file>s are given, it reformats the files. If -i is specified\n" diff --git a/tools/driver/cc1_main.cpp b/tools/driver/cc1_main.cpp index 7d21c6a671d21..7315a1357089c 100644 --- a/tools/driver/cc1_main.cpp +++ b/tools/driver/cc1_main.cpp @@ -1,9 +1,8 @@ //===-- cc1_main.cpp - Clang CC1 Compiler Frontend ------------------------===// // -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // @@ -14,6 +13,7 @@ //===----------------------------------------------------------------------===// #include "clang/Basic/Stack.h" +#include "clang/Basic/TargetOptions.h" #include "clang/CodeGen/ObjectFilePCHContainerOperations.h" #include "clang/Config/config.h" #include "clang/Driver/DriverDiagnostic.h" @@ -35,10 +35,14 @@ #include "llvm/Support/Compiler.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/ManagedStatic.h" +#include "llvm/Support/Path.h" #include "llvm/Support/Signals.h" +#include "llvm/Support/TargetRegistry.h" #include "llvm/Support/TargetSelect.h" +#include "llvm/Support/TimeProfiler.h" #include "llvm/Support/Timer.h" #include "llvm/Support/raw_ostream.h" +#include "llvm/Target/TargetMachine.h" #include <cstdio> #ifdef CLANG_HAVE_RLIMITS @@ -165,6 +169,23 @@ static void ensureSufficientStack() { static void ensureSufficientStack() {} #endif +/// Print supported cpus of the given target. +static int PrintSupportedCPUs(std::string TargetStr) { + std::string Error; + const llvm::Target *TheTarget = + llvm::TargetRegistry::lookupTarget(TargetStr, Error); + if (!TheTarget) { + llvm::errs() << Error; + return 1; + } + + // the target machine will handle the mcpu printing + llvm::TargetOptions Options; + std::unique_ptr<llvm::TargetMachine> TheTargetMachine( + TheTarget->createTargetMachine(TargetStr, "", "+cpuHelp", Options, None)); + return 0; +} + int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) { ensureSufficientStack(); @@ -195,6 +216,13 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) { bool Success = CompilerInvocation::CreateFromArgs( Clang->getInvocation(), Argv.begin(), Argv.end(), Diags); + if (Clang->getFrontendOpts().TimeTrace) + llvm::timeTraceProfilerInitialize(); + + // --print-supported-cpus takes priority over the actual compilation. + if (Clang->getFrontendOpts().PrintSupportedCPUs) + return PrintSupportedCPUs(Clang->getTargetOpts().Triple); + // Infer the builtin include path if unspecified. if (Clang->getHeaderSearchOpts().UseBuiltinIncludes && Clang->getHeaderSearchOpts().ResourceDir.empty()) @@ -216,12 +244,36 @@ int cc1_main(ArrayRef<const char *> Argv, const char *Argv0, void *MainAddr) { return 1; // Execute the frontend actions. - Success = ExecuteCompilerInvocation(Clang.get()); + { + llvm::TimeTraceScope TimeScope("ExecuteCompiler", StringRef("")); + Success = ExecuteCompilerInvocation(Clang.get()); + } // If any timers were active but haven't been destroyed yet, print their // results now. This happens in -disable-free mode. llvm::TimerGroup::printAll(llvm::errs()); + if (llvm::timeTraceProfilerEnabled()) { + SmallString<128> Path(Clang->getFrontendOpts().OutputFile); + llvm::sys::path::replace_extension(Path, "json"); + auto profilerOutput = + Clang->createOutputFile(Path.str(), + /*Binary=*/false, + /*RemoveFileOnSignal=*/false, "", + /*Extension=*/"json", + /*useTemporary=*/false); + + llvm::timeTraceProfilerWrite(*profilerOutput); + // FIXME(ibiryukov): make profilerOutput flush in destructor instead. + profilerOutput->flush(); + llvm::timeTraceProfilerCleanup(); + + llvm::errs() << "Time trace json-file dumped to " << Path.str() << "\n"; + llvm::errs() + << "Use chrome://tracing or Speedscope App " + "(https://www.speedscope.app) for flamegraph visualization\n"; + } + // Our error handler depends on the Diagnostics object, which we're // potentially about to delete. Uninstall the handler now so that any // later errors use the default handling behavior instead. diff --git a/tools/driver/cc1as_main.cpp b/tools/driver/cc1as_main.cpp index be4f174ed412a..a2902b6aa9263 100644 --- a/tools/driver/cc1as_main.cpp +++ b/tools/driver/cc1as_main.cpp @@ -1,9 +1,8 @@ //===-- cc1as_main.cpp - Clang Assembler ---------------------------------===// // -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // @@ -99,7 +98,7 @@ struct AssemblerInvocation { llvm::DebugCompressionType CompressDebugSections = llvm::DebugCompressionType::None; std::string MainFileName; - std::string SplitDwarfFile; + std::string SplitDwarfOutput; /// @} /// @name Frontend Options @@ -138,6 +137,10 @@ struct AssemblerInvocation { /// The name of the relocation model to use. std::string RelocationModel; + /// The ABI targeted by the backend. Specified using -target-abi. Empty + /// otherwise. + std::string TargetABI; + /// @} public: @@ -255,7 +258,7 @@ bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts, } Opts.LLVMArgs = Args.getAllArgValues(OPT_mllvm); Opts.OutputPath = Args.getLastArgValue(OPT_o); - Opts.SplitDwarfFile = Args.getLastArgValue(OPT_split_dwarf_file); + Opts.SplitDwarfOutput = Args.getLastArgValue(OPT_split_dwarf_output); if (Arg *A = Args.getLastArg(OPT_filetype)) { StringRef Name = A->getValue(); unsigned OutputType = StringSwitch<unsigned>(Name) @@ -283,6 +286,7 @@ bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts, Opts.NoExecStack = Args.hasArg(OPT_mno_exec_stack); Opts.FatalWarnings = Args.hasArg(OPT_massembler_fatal_warnings); Opts.RelocationModel = Args.getLastArgValue(OPT_mrelocation_model, "pic"); + Opts.TargetABI = Args.getLastArgValue(OPT_target_abi); Opts.IncrementalLinkerCompatible = Args.hasArg(OPT_mincremental_linker_compatible); Opts.SymbolDefs = Args.getAllArgValues(OPT_defsym); @@ -337,7 +341,7 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts, SourceMgr SrcMgr; // Tell SrcMgr about this buffer, which is what the parser will pick up. - SrcMgr.AddNewSourceBuffer(std::move(*Buffer), SMLoc()); + unsigned BufferIndex = SrcMgr.AddNewSourceBuffer(std::move(*Buffer), SMLoc()); // Record the location of the include directories so that the lexer can find // it later. @@ -363,8 +367,8 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts, if (!FDOS) return true; std::unique_ptr<raw_fd_ostream> DwoOS; - if (!Opts.SplitDwarfFile.empty()) - DwoOS = getOutputStream(Opts.SplitDwarfFile, Diags, IsBinary); + if (!Opts.SplitDwarfOutput.empty()) + DwoOS = getOutputStream(Opts.SplitDwarfOutput, Diags, IsBinary); // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and // MCObjectFileInfo needs a MCContext reference in order to initialize itself. @@ -394,12 +398,21 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts, Ctx.setDwarfDebugProducer(StringRef(Opts.DwarfDebugProducer)); if (!Opts.DebugCompilationDir.empty()) Ctx.setCompilationDir(Opts.DebugCompilationDir); + else { + // If no compilation dir is set, try to use the current directory. + SmallString<128> CWD; + if (!sys::fs::current_path(CWD)) + Ctx.setCompilationDir(CWD); + } if (!Opts.DebugPrefixMap.empty()) for (const auto &KV : Opts.DebugPrefixMap) Ctx.addDebugPrefixMapEntry(KV.first, KV.second); if (!Opts.MainFileName.empty()) Ctx.setMainFileName(StringRef(Opts.MainFileName)); Ctx.setDwarfVersion(Opts.DwarfVersion); + if (Opts.GenDwarfForAssembly) + Ctx.setGenDwarfRootFile(Opts.InputFile, + SrcMgr.getMemoryBuffer(BufferIndex)->getBuffer()); // Build up the feature string from the target feature list. std::string FS; @@ -418,6 +431,9 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts, raw_pwrite_stream *Out = FDOS.get(); std::unique_ptr<buffer_ostream> BOS; + MCTargetOptions MCOptions; + MCOptions.ABIName = Opts.TargetABI; + // FIXME: There is a bit of code duplication with addPassesToEmitFile. if (Opts.OutputType == AssemblerInvocation::FT_Asm) { MCInstPrinter *IP = TheTarget->createMCInstPrinter( @@ -426,7 +442,6 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts, std::unique_ptr<MCCodeEmitter> CE; if (Opts.ShowEncoding) CE.reset(TheTarget->createMCCodeEmitter(*MCII, *MRI, Ctx)); - MCTargetOptions MCOptions; std::unique_ptr<MCAsmBackend> MAB( TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions)); @@ -447,7 +462,6 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts, std::unique_ptr<MCCodeEmitter> CE( TheTarget->createMCCodeEmitter(*MCII, *MRI, Ctx)); - MCTargetOptions MCOptions; std::unique_ptr<MCAsmBackend> MAB( TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions)); std::unique_ptr<MCObjectWriter> OW = @@ -481,9 +495,8 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts, createMCAsmParser(SrcMgr, Ctx, *Str.get(), *MAI)); // FIXME: init MCTargetOptions from sanitizer flags here. - MCTargetOptions Options; std::unique_ptr<MCTargetAsmParser> TAP( - TheTarget->createMCAsmParser(*STI, *Parser, *MCII, Options)); + TheTarget->createMCAsmParser(*STI, *Parser, *MCII, MCOptions)); if (!TAP) Failed = Diags.Report(diag::err_target_unknown_triple) << Opts.Triple; @@ -514,8 +527,8 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts, if (Failed) { if (Opts.OutputPath != "-") sys::fs::remove(Opts.OutputPath); - if (!Opts.SplitDwarfFile.empty() && Opts.SplitDwarfFile != "-") - sys::fs::remove(Opts.SplitDwarfFile); + if (!Opts.SplitDwarfOutput.empty() && Opts.SplitDwarfOutput != "-") + sys::fs::remove(Opts.SplitDwarfOutput); } return Failed; diff --git a/tools/driver/cc1gen_reproducer_main.cpp b/tools/driver/cc1gen_reproducer_main.cpp index 2d7ea86f7486d..4aadab7301bc9 100644 --- a/tools/driver/cc1gen_reproducer_main.cpp +++ b/tools/driver/cc1gen_reproducer_main.cpp @@ -1,9 +1,8 @@ //===-- cc1gen_reproducer_main.cpp - Clang reproducer generator ----------===// // -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // diff --git a/tools/driver/driver.cpp b/tools/driver/driver.cpp index 51143e3d8cb75..dfc96d308ab50 100644 --- a/tools/driver/driver.cpp +++ b/tools/driver/driver.cpp @@ -1,9 +1,8 @@ //===-- driver.cpp - Clang GCC-Compatible Driver --------------------------===// // -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // @@ -341,7 +340,7 @@ int main(int argc_, const char **argv_) { // response files written by clang will tokenize the same way in either mode. bool ClangCLMode = false; if (StringRef(TargetAndMode.DriverMode).equals("--driver-mode=cl") || - std::find_if(argv.begin(), argv.end(), [](const char *F) { + llvm::find_if(argv, [](const char *F) { return F && strcmp(F, "--driver-mode=cl") == 0; }) != argv.end()) { ClangCLMode = true; |