[AMDGPU][AsmParser][NFC] Translate parsed MIMG instructions to MCInsts automatically.
[llvm-project.git] / clang-tools-extra / include-cleaner / lib / IncludeSpeller.cpp
blobe29a8104c0ae856300821a08014c669395ad3b66
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 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>());
40 return Result;
41 }();
43 std::string Spelling;
44 for (const auto &Speller : Spellers) {
45 Spelling = (*Speller)(Input);
46 if (!Spelling.empty())
47 break;
49 return Spelling;
51 } // namespace
53 std::string spellHeader(const IncludeSpeller::Input &Input) {
54 const Header &H = Input.H;
55 switch (H.kind()) {
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