| 1 | //===- Utils.cpp - Shared utilities for SSAF tools -----------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "clang/ScalableStaticAnalysisFramework/Tool/Utils.h" |
| 10 | #include "llvm/ADT/STLExtras.h" |
| 11 | #include "llvm/ADT/SmallVector.h" |
| 12 | #include "llvm/Support/CommandLine.h" |
| 13 | #include "llvm/Support/DynamicLibrary.h" |
| 14 | #include "llvm/Support/Error.h" |
| 15 | #include "llvm/Support/FormatVariadic.h" |
| 16 | #include "llvm/Support/Path.h" |
| 17 | #include "llvm/Support/Process.h" |
| 18 | #include "llvm/Support/WithColor.h" |
| 19 | #include "llvm/Support/raw_ostream.h" |
| 20 | #include <cassert> |
| 21 | #include <memory> |
| 22 | #include <string> |
| 23 | |
| 24 | using namespace clang; |
| 25 | using namespace ssaf; |
| 26 | |
| 27 | namespace path = llvm::sys::path; |
| 28 | |
| 29 | namespace { |
| 30 | |
| 31 | llvm::StringRef ToolName; |
| 32 | llvm::StringRef ToolVersion; |
| 33 | |
| 34 | void printVersion(llvm::raw_ostream &OS) { |
| 35 | OS << ToolName << " " << ToolVersion << "\n" ; |
| 36 | } |
| 37 | |
| 38 | // Returns the SerializationFormat registered for \p Extension, or nullptr if |
| 39 | // none is registered. Results are cached for the lifetime of the process. |
| 40 | // FIXME: This will be revisited after we add support for registering formats |
| 41 | // with extensions. |
| 42 | SerializationFormat *getFormatForExtension(llvm::StringRef Extension) { |
| 43 | // This cache is not thread-safe. SSAF tools are single-threaded CLIs, so |
| 44 | // concurrent calls to this function are not expected. |
| 45 | |
| 46 | // Realistically, we don't expect to encounter more than four registered |
| 47 | // formats. |
| 48 | static llvm::SmallVector< |
| 49 | std::pair<std::string, std::unique_ptr<SerializationFormat>>, 4> |
| 50 | ExtensionFormatList; |
| 51 | |
| 52 | // Most recently used format is most likely to be reused again. |
| 53 | auto ReversedList = llvm::reverse(C&: ExtensionFormatList); |
| 54 | auto It = llvm::find_if(Range&: ReversedList, P: [&](const auto &Entry) { |
| 55 | return Entry.first == Extension; |
| 56 | }); |
| 57 | if (It != ReversedList.end()) { |
| 58 | return It->second.get(); |
| 59 | } |
| 60 | |
| 61 | if (!isFormatRegistered(FormatName: Extension)) { |
| 62 | return nullptr; |
| 63 | } |
| 64 | |
| 65 | auto Format = makeFormat(FormatName: Extension); |
| 66 | SerializationFormat *Result = Format.get(); |
| 67 | assert(Result && |
| 68 | "makeFormat must return non-null for a registered extension" ); |
| 69 | |
| 70 | ExtensionFormatList.emplace_back(Args&: Extension, Args: std::move(Format)); |
| 71 | |
| 72 | return Result; |
| 73 | } |
| 74 | |
| 75 | } // namespace |
| 76 | |
| 77 | llvm::StringRef ssaf::getToolName() { return ToolName; } |
| 78 | |
| 79 | [[noreturn]] void ssaf::fail(const char *Msg) { |
| 80 | llvm::WithColor::error(OS&: llvm::errs(), Prefix: ToolName) << Msg << "\n" ; |
| 81 | llvm::sys::Process::Exit(RetCode: 1); |
| 82 | } |
| 83 | |
| 84 | [[noreturn]] void ssaf::fail(llvm::Error Err) { |
| 85 | std::string Message = llvm::toString(E: std::move(Err)); |
| 86 | ssaf::fail(Msg: Message.data()); |
| 87 | } |
| 88 | |
| 89 | void ssaf::loadPlugins(llvm::ArrayRef<std::string> Paths) { |
| 90 | for (const std::string &PluginPath : Paths) { |
| 91 | std::string ErrMsg; |
| 92 | if (llvm::sys::DynamicLibrary::LoadLibraryPermanently(Filename: PluginPath.c_str(), |
| 93 | ErrMsg: &ErrMsg)) { |
| 94 | fail(Fmt: ErrorMessages::FailedToLoadPlugin, Args: PluginPath, Args&: ErrMsg); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void ssaf::initTool(int argc, const char **argv, llvm::StringRef Version, |
| 100 | llvm::cl::OptionCategory &Category, |
| 101 | llvm::StringRef ToolHeading) { |
| 102 | // path::stem strips the .exe extension on Windows so ToolName is consistent. |
| 103 | ToolName = path::stem(path: argv[0]); |
| 104 | |
| 105 | // Set tool version for the version printer. |
| 106 | ToolVersion = Version; |
| 107 | |
| 108 | // Hide options unrelated to the tool from --help output. |
| 109 | llvm::cl::HideUnrelatedOptions(Category); |
| 110 | |
| 111 | // Register a custom version printer for the --version flag. |
| 112 | llvm::cl::SetVersionPrinter(printVersion); |
| 113 | |
| 114 | // Parse command-line arguments and exit with an error if they are invalid. |
| 115 | std::string Overview = (ToolHeading + "\n" ).str(); |
| 116 | llvm::cl::ParseCommandLineOptions(argc, argv, Overview); |
| 117 | } |
| 118 | |
| 119 | SummaryFile SummaryFile::fromPath(llvm::StringRef Path) { |
| 120 | llvm::StringRef Extension = path::extension(path: Path); |
| 121 | if (Extension.empty()) { |
| 122 | fail(Fmt: ErrorMessages::CannotValidateSummary, Args&: Path, |
| 123 | Args: ErrorMessages::ExtensionNotSupplied); |
| 124 | } |
| 125 | |
| 126 | Extension = Extension.drop_front(); |
| 127 | SerializationFormat *Format = getFormatForExtension(Extension); |
| 128 | if (!Format) { |
| 129 | std::string BadExtension = |
| 130 | llvm::formatv(Fmt: ErrorMessages::NoFormatForExtension, Vals&: Extension); |
| 131 | fail(Fmt: ErrorMessages::CannotValidateSummary, Args&: Path, Args&: BadExtension); |
| 132 | } |
| 133 | |
| 134 | return {.Path: Path.str(), .Format: Format}; |
| 135 | } |
| 136 | |