VitaNuo updated this revision to Diff 526615.
VitaNuo added a comment.
Remove extra braces.
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D150185/new/
https://reviews.llvm.org/D150185
Files:
clang-tools-extra/clangd/Hover.cpp
clang-tools-extra/clangd/IncludeCleaner.cpp
clang-tools-extra/clangd/IncludeCleaner.h
clang-tools-extra/include-cleaner/include/clang-include-cleaner/Analysis.h
clang-tools-extra/include-cleaner/lib/Analysis.cpp
Index: clang-tools-extra/include-cleaner/lib/Analysis.cpp
===================================================================
--- clang-tools-extra/include-cleaner/lib/Analysis.cpp
+++ clang-tools-extra/include-cleaner/lib/Analysis.cpp
@@ -12,6 +12,7 @@
#include "clang-include-cleaner/Types.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclBase.h"
+#include "clang/Basic/FileEntry.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Format/Format.h"
#include "clang/Lex/HeaderSearch.h"
@@ -20,14 +21,42 @@
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/STLFunctionalExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/Support/Error.h"
+#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/Registry.h"
+#include <functional>
+#include <memory>
#include <string>
+LLVM_INSTANTIATE_REGISTRY(clang::include_cleaner::IncludeSpellingStrategy)
+
namespace clang::include_cleaner {
+std::function<std::string(llvm::StringRef)> defaultHeaderMapper() {
+ return [](llvm::StringRef HeaderPhysicalPath) {
+ static auto Spellers = [] {
+ auto Result =
+ llvm::SmallVector<std::unique_ptr<include_cleaner::IncludeSpeller>>{};
+ for (const auto &Strategy :
+ include_cleaner::IncludeSpellingStrategy::entries())
+ Result.push_back(Strategy.instantiate());
+ return Result;
+ }();
+
+ std::string Result;
+ for (const auto &Speller : Spellers) {
+ Result = (*Speller)(HeaderPhysicalPath);
+ if (!Result.empty())
+ break;
+ }
+ return Result;
+ };
+}
+
void walkUsed(llvm::ArrayRef<Decl *> ASTRoots,
llvm::ArrayRef<SymbolReference> MacroRefs,
const PragmaIncludes *PI, const SourceManager &SM,
@@ -53,19 +82,25 @@
}
}
-std::string spellHeader(const Header &H, HeaderSearch &HS,
- const FileEntry *Main) {
+std::string spellHeader(
+ const Header &H, HeaderSearch &HS, const FileEntry *Main,
+ llvm::function_ref<std::string(llvm::StringRef /*AbsPath*/)> MapHeader) {
switch (H.kind()) {
- case Header::Physical: {
- bool IsSystem = false;
- std::string Path = HS.suggestPathToFileForDiagnostics(
- H.physical(), Main->tryGetRealPathName(), &IsSystem);
- return IsSystem ? "<" + Path + ">" : "\"" + Path + "\"";
- }
case Header::Standard:
return H.standard().name().str();
case Header::Verbatim:
return H.verbatim().str();
+ case Header::Physical:
+ // Spelling physical headers allows for various plug-in strategies.
+ std::string FinalSpelling = MapHeader(H.physical()->tryGetRealPathName());
+ if (!FinalSpelling.empty())
+ return FinalSpelling;
+
+ // Fallback to default spelling via header search.
+ bool IsSystem = false;
+ FinalSpelling = HS.suggestPathToFileForDiagnostics(
+ H.physical(), Main->tryGetRealPathName(), &IsSystem);
+ return IsSystem ? "<" + FinalSpelling + ">" : "\"" + FinalSpelling + "\"";
}
llvm_unreachable("Unknown Header kind");
}
Index: clang-tools-extra/include-cleaner/include/clang-include-cleaner/Analysis.h
===================================================================
--- clang-tools-extra/include-cleaner/include/clang-include-cleaner/Analysis.h
+++ clang-tools-extra/include-cleaner/include/clang-include-cleaner/Analysis.h
@@ -14,11 +14,14 @@
#include "clang-include-cleaner/Record.h"
#include "clang-include-cleaner/Types.h"
#include "clang/Format/Format.h"
+#include "clang/Lex/HeaderSearch.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/STLFunctionalExtras.h"
#include "llvm/ADT/SmallVector.h"
-#include "llvm/Support/MemoryBufferRef.h"
-#include <variant>
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/Registry.h"
+#include <functional>
+#include <string>
namespace clang {
class SourceLocation;
@@ -75,9 +78,6 @@
std::string fixIncludes(const AnalysisResults &Results, llvm::StringRef Code,
const format::FormatStyle &IncludeStyle);
-std::string spellHeader(const Header &H, HeaderSearch &HS,
- const FileEntry *Main);
-
/// Gets all the providers for a symbol by traversing each location.
/// Returned headers are sorted by relevance, first element is the most
/// likely provider for the symbol.
@@ -85,6 +85,31 @@
const SourceManager &SM,
const PragmaIncludes *PI);
+class IncludeSpeller {
+public:
+ virtual ~IncludeSpeller() = default;
+
+ /// An extension point to let applications introduce custom spelling
+ /// strategies for physical headers. It takes in absolute path to a source
+ /// file and should return a verbatim include spelling (with angles/quotes)
+ /// or an empty string to indicate no customizations are needed.
+ virtual std::string operator()(llvm::StringRef HeaderPhysicalPath) const = 0;
+};
+
+typedef llvm::Registry<IncludeSpeller> IncludeSpellingStrategy;
+
+/// A header mapper that iterates over all registered include spelling
+/// strategies. Note that when there are multiple strategies iteration
+/// order is not specified.
+std::function<std::string(llvm::StringRef)> defaultHeaderMapper();
+
+/// Generates a spelling for `H` that can be directly included in `Main`.
+/// When `H` is a physical header, prefers the spelling provided by `MapHeader`,
+/// if any. Otherwise, uses header search info to generate shortest spelling.
+std::string spellHeader(
+ const Header &H, HeaderSearch &HS, const FileEntry *Main,
+ llvm::function_ref<std::string(llvm::StringRef /*AbsPath*/)> MapHeader =
+ defaultHeaderMapper());
} // namespace include_cleaner
} // namespace clang
Index: clang-tools-extra/clangd/IncludeCleaner.h
===================================================================
--- clang-tools-extra/clangd/IncludeCleaner.h
+++ clang-tools-extra/clangd/IncludeCleaner.h
@@ -74,11 +74,6 @@
convertIncludes(const SourceManager &SM,
const llvm::ArrayRef<Inclusion> Includes);
-/// Determines the header spelling of an include-cleaner header
-/// representation. The spelling contains the ""<> characters.
-std::string spellHeader(ParsedAST &AST, const FileEntry *MainFile,
- include_cleaner::Header Provider);
-
std::vector<include_cleaner::SymbolReference>
collectMacroReferences(ParsedAST &AST);
Index: clang-tools-extra/clangd/IncludeCleaner.cpp
===================================================================
--- clang-tools-extra/clangd/IncludeCleaner.cpp
+++ clang-tools-extra/clangd/IncludeCleaner.cpp
@@ -176,7 +176,6 @@
const SourceManager &SM = AST.getSourceManager();
const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID());
-
auto FileStyle = format::getStyle(
format::DefaultFormatStyle, AST.tuPath(), format::DefaultFallbackStyle,
Code, &SM.getFileManager().getVirtualFileSystem());
@@ -197,8 +196,9 @@
continue;
}
- std::string Spelling =
- spellHeader(AST, MainFile, SymbolWithMissingInclude.Providers.front());
+ std::string Spelling = include_cleaner::spellHeader(
+ SymbolWithMissingInclude.Providers.front(),
+ AST.getPreprocessor().getHeaderSearchInfo(), MainFile);
llvm::StringRef HeaderRef{Spelling};
bool Angled = HeaderRef.starts_with("<");
// We might suggest insertion of an existing include in edge cases, e.g.,
@@ -332,21 +332,6 @@
return ConvertedIncludes;
}
-std::string spellHeader(ParsedAST &AST, const FileEntry *MainFile,
- include_cleaner::Header Provider) {
- if (Provider.kind() == include_cleaner::Header::Physical) {
- if (auto CanonicalPath = getCanonicalPath(Provider.physical()->getLastRef(),
- AST.getSourceManager())) {
- std::string SpelledHeader =
- llvm::cantFail(URI::includeSpelling(URI::create(*CanonicalPath)));
- if (!SpelledHeader.empty())
- return SpelledHeader;
- }
- }
- return include_cleaner::spellHeader(
- Provider, AST.getPreprocessor().getHeaderSearchInfo(), MainFile);
-}
-
std::vector<const Inclusion *>
getUnused(ParsedAST &AST,
const llvm::DenseSet<IncludeStructure::HeaderID> &ReferencedFiles,
Index: clang-tools-extra/clangd/Hover.cpp
===================================================================
--- clang-tools-extra/clangd/Hover.cpp
+++ clang-tools-extra/clangd/Hover.cpp
@@ -1222,7 +1222,9 @@
// on local variables, etc.
return;
- HI.Provider = spellHeader(AST, SM.getFileEntryForID(SM.getMainFileID()), H);
+ HI.Provider = include_cleaner::spellHeader(
+ H, AST.getPreprocessor().getHeaderSearchInfo(),
+ SM.getFileEntryForID(SM.getMainFileID()));
}
// FIXME: similar functions are present in FindHeaders.cpp (symbolName)
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits