1 | //===--- IncludeSpeller.cpp------------------------------------------------===// |
---|---|
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-include-cleaner/IncludeSpeller.h" |
10 | #include "clang-include-cleaner/Types.h" |
11 | #include "llvm/ADT/SmallVector.h" |
12 | #include "llvm/Support/ErrorHandling.h" |
13 | #include "llvm/Support/Registry.h" |
14 | #include <memory> |
15 | #include <string> |
16 | |
17 | LLVM_INSTANTIATE_REGISTRY(clang::include_cleaner::IncludeSpellingStrategy) |
18 | |
19 | namespace clang::include_cleaner { |
20 | namespace { |
21 | |
22 | // Fallback strategy to default spelling via header search. |
23 | class DefaultIncludeSpeller : public IncludeSpeller { |
24 | public: |
25 | std::string operator()(const Input &Input) const override { |
26 | switch (Input.H.kind()) { |
27 | case Header::Standard: |
28 | return Input.H.standard().name().str(); |
29 | case Header::Verbatim: |
30 | return Input.H.verbatim().str(); |
31 | case Header::Physical: |
32 | bool IsAngled = false; |
33 | std::string FinalSpelling = Input.HS.suggestPathToFileForDiagnostics( |
34 | File: Input.H.physical(), MainFile: Input.Main->tryGetRealPathName(), IsAngled: &IsAngled); |
35 | return IsAngled ? "<"+ FinalSpelling + ">": "\""+ FinalSpelling + "\""; |
36 | } |
37 | llvm_unreachable("Unknown clang::include_cleaner::Header::Kind enum"); |
38 | } |
39 | }; |
40 | |
41 | } // namespace |
42 | |
43 | std::string spellHeader(const IncludeSpeller::Input &Input) { |
44 | static auto *Spellers = [] { |
45 | auto *Result = |
46 | new llvm::SmallVector<std::unique_ptr<include_cleaner::IncludeSpeller>>; |
47 | for (const auto &Strategy : |
48 | include_cleaner::IncludeSpellingStrategy::entries()) |
49 | Result->push_back(Elt: Strategy.instantiate()); |
50 | Result->push_back(Elt: std::make_unique<DefaultIncludeSpeller>()); |
51 | return Result; |
52 | }(); |
53 | |
54 | std::string Spelling; |
55 | for (const auto &Speller : *Spellers) { |
56 | Spelling = (*Speller)(Input); |
57 | if (!Spelling.empty()) |
58 | break; |
59 | } |
60 | return Spelling; |
61 | } |
62 | |
63 | } // namespace clang::include_cleaner |