| 1 | //===- LinkCLI.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 the link action of `clang-ssaf-linker`. |
| 10 | // Links TU summaries, static libraries, and members of multi-arch static |
| 11 | // libraries into one LU summary. |
| 12 | // |
| 13 | // The class is intentionally independent of the tool's cl::opt globals. |
| 14 | // Every input it needs is passed to run(), so the class can be reused or |
| 15 | // unit-tested outside the driver. |
| 16 | // |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
| 19 | #ifndef LLVM_CLANG_TOOLS_CLANG_SSAF_LINKER_LINKCLI_H |
| 20 | #define LLVM_CLANG_TOOLS_CLANG_SSAF_LINKER_LINKCLI_H |
| 21 | |
| 22 | #include "clang/ScalableStaticAnalysis/Core/EntityLinker/EntityLinker.h" |
| 23 | #include "clang/ScalableStaticAnalysis/Core/EntityLinker/LUSummaryEncoding.h" |
| 24 | #include "clang/ScalableStaticAnalysis/Core/Serialization/SerializationFormat.h" |
| 25 | #include "clang/ScalableStaticAnalysis/Tool/Utils.h" |
| 26 | #include "llvm/ADT/ArrayRef.h" |
| 27 | #include "llvm/ADT/StringRef.h" |
| 28 | #include "llvm/Support/Timer.h" |
| 29 | #include "llvm/TargetParser/Triple.h" |
| 30 | #include <cstddef> |
| 31 | #include <optional> |
| 32 | #include <string> |
| 33 | #include <vector> |
| 34 | |
| 35 | namespace clang::ssaf { |
| 36 | |
| 37 | /// Runs the default linking action for `clang-ssaf-linker`. |
| 38 | class LinkCLI { |
| 39 | public: |
| 40 | /// Orchestrates validation, linking, and serialization of the LU summary. |
| 41 | /// Non-recoverable errors call fail() from Tool/Utils.h and terminate the |
| 42 | /// process. |
| 43 | void run(llvm::TimerGroup &TG, llvm::ArrayRef<std::string> InputPaths, |
| 44 | llvm::StringRef OutputPath, llvm::StringRef TargetTriple, |
| 45 | bool Verbose, bool Time); |
| 46 | |
| 47 | private: |
| 48 | /// Validates the output path and every input path, derives the link unit |
| 49 | /// name, and validates TargetTriple if it is set. |
| 50 | void validate(unsigned Level, llvm::Timer &TValidate); |
| 51 | |
| 52 | /// Reads the inputs and folds each into one link unit, in command line |
| 53 | /// order. |
| 54 | /// |
| 55 | /// \returns The accumulated LU summary. |
| 56 | LUSummaryEncoding link(unsigned Level, llvm::Timer &TRead, |
| 57 | llvm::Timer &TLink); |
| 58 | |
| 59 | /// Reads the artifact from \p Input. |
| 60 | /// |
| 61 | /// \param Index The input's position, reported as the note's [i/N] counter. |
| 62 | ArtifactEncoding readInput(const FormatFile &Input, size_t Index, |
| 63 | unsigned Level, llvm::Timer &TRead); |
| 64 | |
| 65 | /// Determines the link unit's target triple. |
| 66 | /// |
| 67 | /// An explicit --target-triple wins. Otherwise the triple is inferred from |
| 68 | /// \p First: its own for a TU summary or a static library, and its sole |
| 69 | /// member's for a single-member multi-arch static library. Any other shape |
| 70 | /// cannot be inferred from and requires --target-triple. |
| 71 | /// |
| 72 | /// \param SourceFile The path \p First was read from, named in diagnostics. |
| 73 | llvm::Triple resolveTargetTriple(const ArtifactEncoding &First, |
| 74 | llvm::StringRef SourceFile, unsigned Level); |
| 75 | |
| 76 | /// Folds one input into \p EL, reporting whatever EntityLinker rejects -- |
| 77 | /// including an input that does not belong to the resolved target -- with the |
| 78 | /// input's path as context. |
| 79 | /// |
| 80 | /// \param SourceFile The input's path, named in diagnostics and notes. |
| 81 | /// \param Index The input's position, reported as the note's [i/N] counter. |
| 82 | void linkInput(EntityLinker &EL, ArtifactEncoding Encoding, |
| 83 | llvm::StringRef SourceFile, size_t Index, unsigned Level, |
| 84 | llvm::Timer &TLink); |
| 85 | |
| 86 | /// Serializes the LU summary to the validated output path. |
| 87 | void write(const LUSummaryEncoding &Output, unsigned Level, |
| 88 | llvm::Timer &TWrite); |
| 89 | |
| 90 | // Arguments captured by run() before dispatching to linking methods. |
| 91 | // InputPaths, OutputPath, and TargetTriple are non-owning: they alias the |
| 92 | // driver's cl::opt storage, which outlives the call. |
| 93 | llvm::ArrayRef<std::string> InputPaths; |
| 94 | llvm::StringRef OutputPath; |
| 95 | llvm::StringRef TargetTriple; |
| 96 | bool Verbose = false; |
| 97 | bool Time = false; |
| 98 | |
| 99 | // State populated during validate() and consumed by later phases. |
| 100 | FormatFile OutputFile; |
| 101 | std::vector<FormatFile> InputFiles; |
| 102 | std::string LinkUnitName; |
| 103 | |
| 104 | // The triple from --target-triple, parsed and validated by validate(), or |
| 105 | // nullopt when the flag is not supplied. |
| 106 | std::optional<llvm::Triple> ExplicitTriple; |
| 107 | }; |
| 108 | |
| 109 | } // namespace clang::ssaf |
| 110 | |
| 111 | #endif // LLVM_CLANG_TOOLS_CLANG_SSAF_LINKER_LINKCLI_H |
| 112 | |