| 1 |
//===--- Types.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/Types.h" |
| 10 |
#include "TypesInternal.h" |
| 11 |
#include "clang/AST/Decl.h" |
| 12 |
#include "clang/Basic/FileEntry.h" |
| 13 |
#include "llvm/ADT/StringExtras.h" |
| 14 |
#include "llvm/Support/raw_ostream.h" |
| 15 |
|
| 16 |
namespace clang::include_cleaner { |
| 17 |
|
| 18 |
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Symbol &S) { |
| 19 |
switch (S.kind()) { |
| 20 |
case Symbol::Declaration: |
| 21 |
if (const auto *ND = llvm::dyn_cast<NamedDecl>(&S.declaration())) |
| 22 |
return OS << ND->getQualifiedNameAsString(); |
| 23 |
return OS << S.declaration().getDeclKindName(); |
| 24 |
case Symbol::Macro: |
| 25 |
return OS << S.macro().Name->getName(); |
| 26 |
} |
| 27 |
llvm_unreachable("Unhandled Symbol kind"); |
| 28 |
} |
| 29 |
|
| 30 |
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Header &H) { |
| 31 |
switch (H.kind()) { |
| 32 |
case Header::Physical: |
| 33 |
return OS << H.physical()->getName(); |
| 34 |
case Header::Standard: |
| 35 |
return OS << H.standard().name(); |
| 36 |
case Header::Verbatim: |
| 37 |
return OS << H.verbatim(); |
| 38 |
} |
| 39 |
llvm_unreachable("Unhandled Header kind"); |
| 40 |
} |
| 41 |
|
| 42 |
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Include &I) { |
| 43 |
return OS << I.Line << ": " << I.quote() << " => " |
| 44 |
<< (I.Resolved ? I.Resolved->getName() : "<missing>"); |
| 45 |
} |
| 46 |
|
| 47 |
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const SymbolReference &R) { |
| 48 |
// We can't decode the Location without SourceManager. Its raw representation |
| 49 |
// isn't completely useless (and distinguishes SymbolReference from Symbol). |
| 50 |
return OS << R.RT << " reference to " << R.Target << "@0x" |
| 51 |
<< llvm::utohexstr( |
| 52 |
R.RefLocation.getRawEncoding(), /*LowerCase=*/false, |
| 53 |
/*Width=*/CHAR_BIT * sizeof(SourceLocation::UIntTy)); |
| 54 |
} |
| 55 |
|
| 56 |
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, RefType T) { |
| 57 |
switch (T) { |
| 58 |
case RefType::Explicit: |
| 59 |
return OS << "explicit"; |
| 60 |
case RefType::Implicit: |
| 61 |
return OS << "implicit"; |
| 62 |
case RefType::Ambiguous: |
| 63 |
return OS << "ambiguous"; |
| 64 |
} |
| 65 |
llvm_unreachable("Unexpected RefType"); |
| 66 |
} |
| 67 |
|
| 68 |
std::string Include::quote() const { |
| 69 |
return (llvm::StringRef(Angled ? "<" : "\"") + Spelled + |
| 70 |
(Angled ? ">" : "\"")) |
| 71 |
.str(); |
| 72 |
} |
| 73 |
|
| 74 |
void Includes::add(const Include &I) { |
| 75 |
unsigned Index = All.size(); |
| 76 |
All.push_back(I); |
| 77 |
auto BySpellingIt = BySpelling.try_emplace(I.Spelled).first; |
| 78 |
All.back().Spelled = BySpellingIt->first(); // Now we own the backing string. |
| 79 |
|
| 80 |
BySpellingIt->second.push_back(Index); |
| 81 |
if (I.Resolved) |
| 82 |
ByFile[I.Resolved].push_back(Index); |
| 83 |
ByLine[I.Line] = Index; |
| 84 |
} |
| 85 |
|
| 86 |
const Include *Includes::atLine(unsigned OneBasedIndex) const { |
| 87 |
auto It = ByLine.find(OneBasedIndex); |
| 88 |
return (It == ByLine.end()) ? nullptr : &All[It->second]; |
| 89 |
} |
| 90 |
|
| 91 |
llvm::SmallVector<const Include *> Includes::match(Header H) const { |
| 92 |
llvm::SmallVector<const Include *> Result; |
| 93 |
switch (H.kind()) { |
| 94 |
case Header::Physical: |
| 95 |
for (unsigned I : ByFile.lookup(H.physical())) |
| 96 |
Result.push_back(&All[I]); |
| 97 |
break; |
| 98 |
case Header::Standard: |
| 99 |
for (unsigned I : BySpelling.lookup(H.standard().name().trim("<>"))) |
| 100 |
Result.push_back(&All[I]); |
| 101 |
break; |
| 102 |
case Header::Verbatim: |
| 103 |
for (unsigned I : BySpelling.lookup(H.verbatim().trim("\"<>"))) |
| 104 |
Result.push_back(&All[I]); |
| 105 |
break; |
| 106 |
} |
| 107 |
return Result; |
| 108 |
} |
| 109 |
|
| 110 |
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const SymbolLocation &S) { |
| 111 |
switch (S.kind()) { |
| 112 |
case SymbolLocation::Physical: |
| 113 |
// We can't decode the Location without SourceManager. Its raw |
| 114 |
// representation isn't completely useless (and distinguishes |
| 115 |
// SymbolReference from Symbol). |
| 116 |
return OS << "@0x" |
| 117 |
<< llvm::utohexstr( |
| 118 |
S.physical().getRawEncoding(), /*LowerCase=*/false, |
| 119 |
/*Width=*/CHAR_BIT * sizeof(SourceLocation::UIntTy)); |
| 120 |
case SymbolLocation::Standard: |
| 121 |
return OS << S.standard().scope() << S.standard().name(); |
| 122 |
} |
| 123 |
llvm_unreachable("Unhandled Symbol kind"); |
| 124 |
} |
| 125 |
|
| 126 |
bool Header::operator<(const Header &RHS) const { |
| 127 |
if (kind() != RHS.kind()) |
| 128 |
return kind() < RHS.kind(); |
| 129 |
switch (kind()) { |
| 130 |
case Header::Physical: |
| 131 |
return physical()->getName() < RHS.physical()->getName(); |
| 132 |
case Header::Standard: |
| 133 |
return standard().name() < RHS.standard().name(); |
| 134 |
case Header::Verbatim: |
| 135 |
return verbatim() < RHS.verbatim(); |
| 136 |
} |
| 137 |
llvm_unreachable("unhandled Header kind"); |
| 138 |
} |
| 139 |
} // namespace clang::include_cleaner |