[clang][modules] Don't prevent translation of FW_Private includes when explicitly...
[llvm-project.git] / clang-tools-extra / include-cleaner / lib / IncludeSpeller.cpp
blob2073f0a1d3d8786d59f5c1d017eecf27cf0bbc8c
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 //===----------------------------------------------------------------------===//
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>
17 LLVM_INSTANTIATE_REGISTRY(clang::include_cleaner::IncludeSpellingStrategy)
19 namespace clang::include_cleaner {
20 namespace {
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 Input.H.physical(), Input.Main->tryGetRealPathName(), &IsAngled);
35 return IsAngled ? "<" + FinalSpelling + ">" : "\"" + FinalSpelling + "\"";
37 llvm_unreachable("Unknown clang::include_cleaner::Header::Kind enum");
41 } // namespace
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(Strategy.instantiate());
50 Result->push_back(std::make_unique<DefaultIncludeSpeller>());
51 return Result;
52 }();
54 std::string Spelling;
55 for (const auto &Speller : *Spellers) {
56 Spelling = (*Speller)(Input);
57 if (!Spelling.empty())
58 break;
60 return Spelling;
63 } // namespace clang::include_cleaner