2018-02-16 22:15:55 +08:00
|
|
|
//===--- Headers.cpp - Include headers ---------------------------*- C++-*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2018-02-16 22:15:55 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Headers.h"
|
|
|
|
#include "Compiler.h"
|
|
|
|
#include "Logger.h"
|
2018-05-14 20:19:16 +08:00
|
|
|
#include "SourceCode.h"
|
2018-02-16 22:15:55 +08:00
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Frontend/CompilerInvocation.h"
|
|
|
|
#include "clang/Frontend/FrontendActions.h"
|
|
|
|
#include "clang/Lex/HeaderSearch.h"
|
2019-07-03 15:47:19 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2018-02-20 02:48:44 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2018-02-16 22:15:55 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class RecordHeaders : public PPCallbacks {
|
|
|
|
public:
|
2018-07-03 16:09:29 +08:00
|
|
|
RecordHeaders(const SourceManager &SM, IncludeStructure *Out)
|
|
|
|
: SM(SM), Out(Out) {}
|
2018-02-16 22:15:55 +08:00
|
|
|
|
2018-05-14 20:19:16 +08:00
|
|
|
// Record existing #includes - both written and resolved paths. Only #includes
|
|
|
|
// in the main file are collected.
|
|
|
|
void InclusionDirective(SourceLocation HashLoc, const Token & /*IncludeTok*/,
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::StringRef FileName, bool IsAngled,
|
2018-05-14 20:19:16 +08:00
|
|
|
CharSourceRange FilenameRange, const FileEntry *File,
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::StringRef /*SearchPath*/,
|
|
|
|
llvm::StringRef /*RelativePath*/,
|
2018-05-11 03:13:14 +08:00
|
|
|
const Module * /*Imported*/,
|
2018-11-20 18:58:48 +08:00
|
|
|
SrcMgr::CharacteristicKind FileKind) override {
|
2019-07-19 16:33:39 +08:00
|
|
|
if (isInsideMainFile(HashLoc, SM)) {
|
2018-11-20 18:58:48 +08:00
|
|
|
Out->MainFileIncludes.emplace_back();
|
|
|
|
auto &Inc = Out->MainFileIncludes.back();
|
|
|
|
Inc.R = halfOpenToRange(SM, FilenameRange);
|
|
|
|
Inc.Written =
|
|
|
|
(IsAngled ? "<" + FileName + ">" : "\"" + FileName + "\"").str();
|
|
|
|
Inc.Resolved = File ? File->tryGetRealPathName() : "";
|
|
|
|
Inc.HashOffset = SM.getFileOffset(HashLoc);
|
|
|
|
Inc.FileKind = FileKind;
|
|
|
|
}
|
2018-07-03 16:09:29 +08:00
|
|
|
if (File) {
|
|
|
|
auto *IncludingFileEntry = SM.getFileEntryForID(SM.getFileID(HashLoc));
|
|
|
|
if (!IncludingFileEntry) {
|
|
|
|
assert(SM.getBufferName(HashLoc).startswith("<") &&
|
|
|
|
"Expected #include location to be a file or <built-in>");
|
|
|
|
// Treat as if included from the main file.
|
|
|
|
IncludingFileEntry = SM.getFileEntryForID(SM.getMainFileID());
|
|
|
|
}
|
|
|
|
Out->recordInclude(IncludingFileEntry->getName(), File->getName(),
|
|
|
|
File->tryGetRealPathName());
|
|
|
|
}
|
2018-02-16 22:15:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-05-14 20:19:16 +08:00
|
|
|
const SourceManager &SM;
|
2018-07-03 16:09:29 +08:00
|
|
|
IncludeStructure *Out;
|
2018-02-16 22:15:55 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
bool isLiteralInclude(llvm::StringRef Include) {
|
2018-02-26 16:32:13 +08:00
|
|
|
return Include.startswith("<") || Include.startswith("\"");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HeaderFile::valid() const {
|
|
|
|
return (Verbatim && isLiteralInclude(File)) ||
|
2019-01-07 23:45:19 +08:00
|
|
|
(!Verbatim && llvm::sys::path::is_absolute(File));
|
2018-02-26 16:32:13 +08:00
|
|
|
}
|
|
|
|
|
2019-01-28 22:01:55 +08:00
|
|
|
llvm::Expected<HeaderFile> toHeaderFile(llvm::StringRef Header,
|
|
|
|
llvm::StringRef HintPath) {
|
|
|
|
if (isLiteralInclude(Header))
|
|
|
|
return HeaderFile{Header.str(), /*Verbatim=*/true};
|
|
|
|
auto U = URI::parse(Header);
|
|
|
|
if (!U)
|
|
|
|
return U.takeError();
|
|
|
|
|
|
|
|
auto IncludePath = URI::includeSpelling(*U);
|
|
|
|
if (!IncludePath)
|
|
|
|
return IncludePath.takeError();
|
|
|
|
if (!IncludePath->empty())
|
|
|
|
return HeaderFile{std::move(*IncludePath), /*Verbatim=*/true};
|
|
|
|
|
|
|
|
auto Resolved = URI::resolve(*U, HintPath);
|
|
|
|
if (!Resolved)
|
|
|
|
return Resolved.takeError();
|
|
|
|
return HeaderFile{std::move(*Resolved), /*Verbatim=*/false};
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::SmallVector<llvm::StringRef, 1> getRankedIncludes(const Symbol &Sym) {
|
|
|
|
auto Includes = Sym.IncludeHeaders;
|
|
|
|
// Sort in descending order by reference count and header length.
|
|
|
|
llvm::sort(Includes, [](const Symbol::IncludeHeaderWithReferences &LHS,
|
|
|
|
const Symbol::IncludeHeaderWithReferences &RHS) {
|
|
|
|
if (LHS.References == RHS.References)
|
|
|
|
return LHS.IncludeHeader.size() < RHS.IncludeHeader.size();
|
|
|
|
return LHS.References > RHS.References;
|
|
|
|
});
|
|
|
|
llvm::SmallVector<llvm::StringRef, 1> Headers;
|
|
|
|
for (const auto &Include : Includes)
|
|
|
|
Headers.push_back(Include.IncludeHeader);
|
|
|
|
return Headers;
|
|
|
|
}
|
|
|
|
|
2018-05-14 20:19:16 +08:00
|
|
|
std::unique_ptr<PPCallbacks>
|
2018-07-03 16:09:29 +08:00
|
|
|
collectIncludeStructureCallback(const SourceManager &SM,
|
|
|
|
IncludeStructure *Out) {
|
|
|
|
return llvm::make_unique<RecordHeaders>(SM, Out);
|
|
|
|
}
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
void IncludeStructure::recordInclude(llvm::StringRef IncludingName,
|
|
|
|
llvm::StringRef IncludedName,
|
|
|
|
llvm::StringRef IncludedRealName) {
|
2018-07-03 16:09:29 +08:00
|
|
|
auto Child = fileIndex(IncludedName);
|
|
|
|
if (!IncludedRealName.empty() && RealPathNames[Child].empty())
|
|
|
|
RealPathNames[Child] = IncludedRealName;
|
|
|
|
auto Parent = fileIndex(IncludingName);
|
|
|
|
IncludeChildren[Parent].push_back(Child);
|
|
|
|
}
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
unsigned IncludeStructure::fileIndex(llvm::StringRef Name) {
|
2018-07-03 16:09:29 +08:00
|
|
|
auto R = NameToIndex.try_emplace(Name, RealPathNames.size());
|
|
|
|
if (R.second)
|
|
|
|
RealPathNames.emplace_back();
|
|
|
|
return R.first->getValue();
|
|
|
|
}
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::StringMap<unsigned>
|
|
|
|
IncludeStructure::includeDepth(llvm::StringRef Root) const {
|
2018-07-03 16:09:29 +08:00
|
|
|
// Include depth 0 is the main file only.
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::StringMap<unsigned> Result;
|
2018-07-03 16:09:29 +08:00
|
|
|
Result[Root] = 0;
|
|
|
|
std::vector<unsigned> CurrentLevel;
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::DenseSet<unsigned> Seen;
|
2018-07-03 16:09:29 +08:00
|
|
|
auto It = NameToIndex.find(Root);
|
|
|
|
if (It != NameToIndex.end()) {
|
|
|
|
CurrentLevel.push_back(It->second);
|
|
|
|
Seen.insert(It->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Each round of BFS traversal finds the next depth level.
|
|
|
|
std::vector<unsigned> PreviousLevel;
|
|
|
|
for (unsigned Level = 1; !CurrentLevel.empty(); ++Level) {
|
|
|
|
PreviousLevel.clear();
|
|
|
|
PreviousLevel.swap(CurrentLevel);
|
|
|
|
for (const auto &Parent : PreviousLevel) {
|
|
|
|
for (const auto &Child : IncludeChildren.lookup(Parent)) {
|
|
|
|
if (Seen.insert(Child).second) {
|
|
|
|
CurrentLevel.push_back(Child);
|
|
|
|
const auto &Name = RealPathNames[Child];
|
|
|
|
// Can't include files if we don't have their real path.
|
|
|
|
if (!Name.empty())
|
|
|
|
Result[Name] = Level;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Result;
|
2018-05-14 20:19:16 +08:00
|
|
|
}
|
|
|
|
|
2018-09-27 22:27:02 +08:00
|
|
|
void IncludeInserter::addExisting(const Inclusion &Inc) {
|
|
|
|
IncludedHeaders.insert(Inc.Written);
|
|
|
|
if (!Inc.Resolved.empty())
|
|
|
|
IncludedHeaders.insert(Inc.Resolved);
|
|
|
|
}
|
|
|
|
|
2018-02-16 22:15:55 +08:00
|
|
|
/// FIXME(ioeric): we might not want to insert an absolute include path if the
|
|
|
|
/// path is not shortened.
|
2018-06-15 21:34:18 +08:00
|
|
|
bool IncludeInserter::shouldInsertInclude(
|
2019-04-16 22:35:49 +08:00
|
|
|
PathRef DeclaringHeader, const HeaderFile &InsertedHeader) const {
|
|
|
|
assert(InsertedHeader.valid());
|
2019-04-11 17:36:36 +08:00
|
|
|
if (!HeaderSearchInfo && !InsertedHeader.Verbatim)
|
|
|
|
return false;
|
2019-04-16 22:35:49 +08:00
|
|
|
if (FileName == DeclaringHeader || FileName == InsertedHeader.File)
|
2018-06-15 21:34:18 +08:00
|
|
|
return false;
|
2019-01-07 23:45:19 +08:00
|
|
|
auto Included = [&](llvm::StringRef Header) {
|
2018-05-14 20:19:16 +08:00
|
|
|
return IncludedHeaders.find(Header) != IncludedHeaders.end();
|
2018-02-26 16:32:13 +08:00
|
|
|
};
|
2019-04-16 22:35:49 +08:00
|
|
|
return !Included(DeclaringHeader) && !Included(InsertedHeader.File);
|
2018-06-15 21:34:18 +08:00
|
|
|
}
|
2018-02-26 16:32:13 +08:00
|
|
|
|
2019-07-09 02:07:46 +08:00
|
|
|
llvm::Optional<std::string>
|
2019-07-03 15:47:19 +08:00
|
|
|
IncludeInserter::calculateIncludePath(const HeaderFile &InsertedHeader,
|
|
|
|
llvm::StringRef IncludingFile) const {
|
2019-04-16 22:35:49 +08:00
|
|
|
assert(InsertedHeader.valid());
|
2018-02-26 16:32:13 +08:00
|
|
|
if (InsertedHeader.Verbatim)
|
|
|
|
return InsertedHeader.File;
|
2018-06-15 21:34:18 +08:00
|
|
|
bool IsSystem = false;
|
2019-07-09 02:07:46 +08:00
|
|
|
std::string Suggested;
|
|
|
|
if (HeaderSearchInfo) {
|
|
|
|
Suggested = HeaderSearchInfo->suggestPathToFileForDiagnostics(
|
|
|
|
InsertedHeader.File, BuildDir, IncludingFile, &IsSystem);
|
|
|
|
} else {
|
|
|
|
// Calculate include relative to including file only.
|
|
|
|
StringRef IncludingDir = llvm::sys::path::parent_path(IncludingFile);
|
|
|
|
SmallString<256> RelFile(InsertedHeader.File);
|
|
|
|
// Replacing with "" leaves "/RelFile" if IncludingDir doesn't end in "/".
|
|
|
|
llvm::sys::path::replace_path_prefix(RelFile, IncludingDir, "./");
|
|
|
|
Suggested = llvm::sys::path::convert_to_slash(
|
|
|
|
llvm::sys::path::remove_leading_dotslash(RelFile));
|
|
|
|
}
|
|
|
|
// FIXME: should we allow (some limited number of) "../header.h"?
|
|
|
|
if (llvm::sys::path::is_absolute(Suggested))
|
|
|
|
return None;
|
2018-02-16 22:15:55 +08:00
|
|
|
if (IsSystem)
|
|
|
|
Suggested = "<" + Suggested + ">";
|
|
|
|
else
|
|
|
|
Suggested = "\"" + Suggested + "\"";
|
|
|
|
return Suggested;
|
|
|
|
}
|
|
|
|
|
2019-01-07 23:45:19 +08:00
|
|
|
llvm::Optional<TextEdit>
|
|
|
|
IncludeInserter::insert(llvm::StringRef VerbatimHeader) const {
|
|
|
|
llvm::Optional<TextEdit> Edit = None;
|
2018-06-15 21:34:18 +08:00
|
|
|
if (auto Insertion = Inserter.insert(VerbatimHeader.trim("\"<>"),
|
|
|
|
VerbatimHeader.startswith("<")))
|
|
|
|
Edit = replacementToEdit(Code, *Insertion);
|
|
|
|
return Edit;
|
2018-05-15 23:29:32 +08:00
|
|
|
}
|
|
|
|
|
2018-11-20 18:58:48 +08:00
|
|
|
llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const Inclusion &Inc) {
|
|
|
|
return OS << Inc.Written << " = "
|
|
|
|
<< (Inc.Resolved.empty() ? Inc.Resolved : "[unresolved]") << " at "
|
|
|
|
<< Inc.R;
|
|
|
|
}
|
|
|
|
|
2018-02-16 22:15:55 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|