1 //===--- IncludeSpeller.cpp------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
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"
17 LLVM_INSTANTIATE_REGISTRY(clang::include_cleaner::IncludeSpellingStrategy
)
19 namespace clang::include_cleaner
{
22 // Fallback strategy to default spelling via header search.
23 class DefaultIncludeSpeller
: public IncludeSpeller
{
25 std::string
operator()(const Input
&Input
) const override
{
26 bool IsSystem
= false;
27 std::string FinalSpelling
= Input
.HS
.suggestPathToFileForDiagnostics(
28 Input
.H
.physical(), Input
.Main
->tryGetRealPathName(), &IsSystem
);
29 return IsSystem
? "<" + FinalSpelling
+ ">" : "\"" + FinalSpelling
+ "\"";
33 std::string
spellPhysicalHeader(const IncludeSpeller::Input
&Input
) {
34 static auto Spellers
= [] {
35 llvm::SmallVector
<std::unique_ptr
<include_cleaner::IncludeSpeller
>> Result
;
36 for (const auto &Strategy
:
37 include_cleaner::IncludeSpellingStrategy::entries())
38 Result
.push_back(Strategy
.instantiate());
39 Result
.push_back(std::make_unique
<DefaultIncludeSpeller
>());
44 for (const auto &Speller
: Spellers
) {
45 Spelling
= (*Speller
)(Input
);
46 if (!Spelling
.empty())
53 std::string
spellHeader(const IncludeSpeller::Input
&Input
) {
54 const Header
&H
= Input
.H
;
56 case Header::Standard
:
57 return H
.standard().name().str();
58 case Header::Verbatim
:
59 return H
.verbatim().str();
60 case Header::Physical
:
61 // Spelling physical headers allows for various plug-in strategies.
62 return spellPhysicalHeader(Input
);
64 llvm_unreachable("Unknown Header kind");
66 } // namespace clang::include_cleaner