| 1 | //===- StaticLibraryCreateCLI.h ---------------------------------*- C++ -*-===// |
| 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 | // Declares the CLI action class for `clang-ssaf-linker static-library |
| 10 | // create`: validates inputs and output, streams TU summaries into a |
| 11 | // StaticLibrary one at a time (reading and inserting each in turn), |
| 12 | // and serializes the result. |
| 13 | // |
| 14 | // The class is intentionally independent of the tool's cl::opt globals. |
| 15 | // Every input it needs is passed via a Config struct at run() time, so |
| 16 | // the class can be reused or unit-tested outside the driver. |
| 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
| 20 | #ifndef LLVM_CLANG_TOOLS_CLANG_SSAF_LINKER_STATICLIBRARYCREATECLI_H |
| 21 | #define LLVM_CLANG_TOOLS_CLANG_SSAF_LINKER_STATICLIBRARYCREATECLI_H |
| 22 | |
| 23 | #include "clang/ScalableStaticAnalysis/Core/EntityLinker/StaticLibrary.h" |
| 24 | #include "clang/ScalableStaticAnalysis/Tool/Utils.h" |
| 25 | #include "llvm/ADT/ArrayRef.h" |
| 26 | #include "llvm/ADT/StringRef.h" |
| 27 | #include "llvm/Support/FormatVariadic.h" |
| 28 | #include "llvm/Support/Timer.h" |
| 29 | #include "llvm/Support/WithColor.h" |
| 30 | #include "llvm/Support/raw_ostream.h" |
| 31 | #include "llvm/TargetParser/Triple.h" |
| 32 | #include <optional> |
| 33 | #include <string> |
| 34 | #include <utility> |
| 35 | #include <vector> |
| 36 | |
| 37 | namespace clang::ssaf { |
| 38 | |
| 39 | /// Runs the `static-library create` action for `clang-ssaf-linker`. |
| 40 | class StaticLibraryCreateCLI { |
| 41 | public: |
| 42 | /// Everything the action needs at runtime, threaded in from the CLI |
| 43 | /// driver. StringRef/ArrayRef fields alias the driver's cl::opt storage |
| 44 | /// and must remain valid for the duration of run(). |
| 45 | struct Config { |
| 46 | llvm::ArrayRef<std::string> InputPaths; |
| 47 | llvm::StringRef OutputPath; |
| 48 | llvm::StringRef Namespace; |
| 49 | llvm::StringRef TargetTriple; |
| 50 | bool Verbose = false; |
| 51 | bool Time = false; |
| 52 | }; |
| 53 | |
| 54 | /// Orchestrator: validate → bundle → write. Non-recoverable errors call |
| 55 | /// fail() from Tool/Utils.h and terminate the process. |
| 56 | void run(llvm::TimerGroup &TG, const Config &Cfg); |
| 57 | |
| 58 | private: |
| 59 | /// Validates the output path, input paths, resolves the namespace name, |
| 60 | /// and validates Cfg.TargetTriple if it is set. |
| 61 | void validate(llvm::TimerGroup &TG); |
| 62 | |
| 63 | /// Reads each validated input file and inserts it into a StaticLibrary |
| 64 | /// one at a time. |
| 65 | /// |
| 66 | /// \returns The assembled StaticLibrary. Terminates the process via |
| 67 | /// fail() on any read error, triple mismatch, or duplicate |
| 68 | /// TUNamespace across inputs. |
| 69 | StaticLibrary bundle(llvm::TimerGroup &TG); |
| 70 | |
| 71 | /// Serializes the StaticLibrary to the validated output path. |
| 72 | void write(llvm::TimerGroup &TG, const StaticLibrary &Result); |
| 73 | |
| 74 | /// Prints one indented note to stderr when Cfg.Verbose is set. |
| 75 | template <typename... Ts> |
| 76 | void info(unsigned Level, const char *Fmt, Ts &&...Args) const { |
| 77 | if (Cfg.Verbose) { |
| 78 | llvm::WithColor::note() |
| 79 | << std::string(Level * IndentationWidth, ' ') << "- " |
| 80 | << llvm::formatv(Fmt, std::forward<Ts>(Args)...) << "\n" ; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | static constexpr unsigned IndentationWidth = 2; |
| 85 | |
| 86 | // Configuration set by run() before dispatching to phase methods. |
| 87 | Config Cfg; |
| 88 | |
| 89 | // State populated during validate() and consumed by later phases. |
| 90 | FormatFile OutputFile; |
| 91 | std::vector<FormatFile> InputFiles; |
| 92 | std::string NamespaceName; |
| 93 | |
| 94 | // Set by validate() only when Cfg.TargetTriple was passed and valid. |
| 95 | // bundle() uses it to construct the StaticLibrary up front; when unset, |
| 96 | // bundle() populates it from the first input, and every subsequent |
| 97 | // input must match. |
| 98 | std::optional<llvm::Triple> ResolvedTriple; |
| 99 | }; |
| 100 | |
| 101 | } // namespace clang::ssaf |
| 102 | |
| 103 | #endif // LLVM_CLANG_TOOLS_CLANG_SSAF_LINKER_STATICLIBRARYCREATECLI_H |
| 104 | |