2018-02-16 22:15:55 +08:00
|
|
|
//===--- Headers.cpp - Include headers ---------------------------*- C++-*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#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"
|
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 {
|
|
|
|
if (SM.isWrittenInMainFile(HashLoc)) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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(
|
|
|
|
const HeaderFile &DeclaringHeader, const HeaderFile &InsertedHeader) const {
|
2018-02-26 16:32:13 +08:00
|
|
|
assert(DeclaringHeader.valid() && InsertedHeader.valid());
|
2018-06-15 21:34:18 +08:00
|
|
|
if (FileName == DeclaringHeader.File || FileName == InsertedHeader.File)
|
|
|
|
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
|
|
|
};
|
2018-06-15 21:34:18 +08:00
|
|
|
return !Included(DeclaringHeader.File) && !Included(InsertedHeader.File);
|
|
|
|
}
|
2018-02-26 16:32:13 +08:00
|
|
|
|
2018-06-15 21:34:18 +08:00
|
|
|
std::string
|
|
|
|
IncludeInserter::calculateIncludePath(const HeaderFile &DeclaringHeader,
|
|
|
|
const HeaderFile &InsertedHeader) const {
|
|
|
|
assert(DeclaringHeader.valid() && 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;
|
2018-02-16 22:15:55 +08:00
|
|
|
std::string Suggested = HeaderSearchInfo.suggestPathToFileForDiagnostics(
|
2018-05-15 23:29:32 +08:00
|
|
|
InsertedHeader.File, BuildDir, &IsSystem);
|
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
|