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 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 Input
.H
.physical(), Input
.Main
->tryGetRealPathName(), &IsAngled
);
35 return IsAngled
? "<" + FinalSpelling
+ ">" : "\"" + FinalSpelling
+ "\"";
37 llvm_unreachable("Unknown clang::include_cleaner::Header::Kind enum");
43 std::string
spellHeader(const IncludeSpeller::Input
&Input
) {
44 static auto *Spellers
= [] {
46 new llvm::SmallVector
<std::unique_ptr
<include_cleaner::IncludeSpeller
>>;
47 for (const auto &Strategy
:
48 include_cleaner::IncludeSpellingStrategy::entries())
49 Result
->push_back(Strategy
.instantiate());
50 Result
->push_back(std::make_unique
<DefaultIncludeSpeller
>());
55 for (const auto &Speller
: *Spellers
) {
56 Spelling
= (*Speller
)(Input
);
57 if (!Spelling
.empty())
63 } // namespace clang::include_cleaner