blob: 58a81dcb29195b3664c30ee86dc4fe2284042e5a [file] [log] [blame]
Eric Liuc5105f92018-02-16 14:15:55 +00001//===--- Headers.cpp - Include headers ---------------------------*- C++-*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "Headers.h"
11#include "Compiler.h"
12#include "Logger.h"
Eric Liu155f5a42018-05-14 12:19:16 +000013#include "SourceCode.h"
Eric Liuc5105f92018-02-16 14:15:55 +000014#include "clang/Frontend/CompilerInstance.h"
15#include "clang/Frontend/CompilerInvocation.h"
16#include "clang/Frontend/FrontendActions.h"
17#include "clang/Lex/HeaderSearch.h"
18#include "clang/Lex/PreprocessorOptions.h"
19#include "clang/Tooling/CompilationDatabase.h"
Eric Liu709bde82018-02-19 18:48:44 +000020#include "llvm/Support/Path.h"
Eric Liuc5105f92018-02-16 14:15:55 +000021
22namespace clang {
23namespace clangd {
24namespace {
25
26class RecordHeaders : public PPCallbacks {
27public:
Eric Liu155f5a42018-05-14 12:19:16 +000028 RecordHeaders(const SourceManager &SM,
29 std::function<void(Inclusion)> Callback)
30 : SM(SM), Callback(std::move(Callback)) {}
Eric Liuc5105f92018-02-16 14:15:55 +000031
Eric Liu155f5a42018-05-14 12:19:16 +000032 // Record existing #includes - both written and resolved paths. Only #includes
33 // in the main file are collected.
34 void InclusionDirective(SourceLocation HashLoc, const Token & /*IncludeTok*/,
Eric Liu6c8e8582018-02-26 08:32:13 +000035 llvm::StringRef FileName, bool IsAngled,
Eric Liu155f5a42018-05-14 12:19:16 +000036 CharSourceRange FilenameRange, const FileEntry *File,
37 llvm::StringRef /*SearchPath*/,
Eric Liuc5105f92018-02-16 14:15:55 +000038 llvm::StringRef /*RelativePath*/,
Julie Hockett546943f2018-05-10 19:13:14 +000039 const Module * /*Imported*/,
40 SrcMgr::CharacteristicKind /*FileType*/) override {
Eric Liu155f5a42018-05-14 12:19:16 +000041 // Only inclusion directives in the main file make sense. The user cannot
42 // select directives not in the main file.
43 if (HashLoc.isInvalid() || !SM.isInMainFile(HashLoc))
44 return;
45 std::string Written =
46 (IsAngled ? "<" + FileName + ">" : "\"" + FileName + "\"").str();
47 std::string Resolved = (!File || File->tryGetRealPathName().empty())
48 ? ""
49 : File->tryGetRealPathName();
50 Callback({halfOpenToRange(SM, FilenameRange), Written, Resolved});
Eric Liuc5105f92018-02-16 14:15:55 +000051 }
52
53private:
Eric Liu155f5a42018-05-14 12:19:16 +000054 const SourceManager &SM;
55 std::function<void(Inclusion)> Callback;
Eric Liuc5105f92018-02-16 14:15:55 +000056};
57
58} // namespace
59
Eric Liu6c8e8582018-02-26 08:32:13 +000060bool isLiteralInclude(llvm::StringRef Include) {
61 return Include.startswith("<") || Include.startswith("\"");
62}
63
64bool HeaderFile::valid() const {
65 return (Verbatim && isLiteralInclude(File)) ||
66 (!Verbatim && llvm::sys::path::is_absolute(File));
67}
68
Eric Liu155f5a42018-05-14 12:19:16 +000069std::unique_ptr<PPCallbacks>
70collectInclusionsInMainFileCallback(const SourceManager &SM,
71 std::function<void(Inclusion)> Callback) {
72 return llvm::make_unique<RecordHeaders>(SM, std::move(Callback));
73}
74
Eric Liuc5105f92018-02-16 14:15:55 +000075/// FIXME(ioeric): we might not want to insert an absolute include path if the
76/// path is not shortened.
77llvm::Expected<std::string>
Eric Liu709bde82018-02-19 18:48:44 +000078calculateIncludePath(llvm::StringRef File, llvm::StringRef Code,
Eric Liu6c8e8582018-02-26 08:32:13 +000079 const HeaderFile &DeclaringHeader,
80 const HeaderFile &InsertedHeader,
Eric Liu709bde82018-02-19 18:48:44 +000081 const tooling::CompileCommand &CompileCommand,
82 IntrusiveRefCntPtr<vfs::FileSystem> FS) {
Eric Liu6c8e8582018-02-26 08:32:13 +000083 assert(llvm::sys::path::is_absolute(File));
84 assert(DeclaringHeader.valid() && InsertedHeader.valid());
85 if (File == DeclaringHeader.File || File == InsertedHeader.File)
Eric Liu709bde82018-02-19 18:48:44 +000086 return "";
87 FS->setCurrentWorkingDirectory(CompileCommand.Directory);
88
Eric Liuc5105f92018-02-16 14:15:55 +000089 // Set up a CompilerInstance and create a preprocessor to collect existing
90 // #include headers in \p Code. Preprocesor also provides HeaderSearch with
91 // which we can calculate the shortest include path for \p Header.
92 std::vector<const char *> Argv;
93 for (const auto &S : CompileCommand.CommandLine)
94 Argv.push_back(S.c_str());
95 IgnoringDiagConsumer IgnoreDiags;
96 auto CI = clang::createInvocationFromCommandLine(
97 Argv,
98 CompilerInstance::createDiagnostics(new DiagnosticOptions(), &IgnoreDiags,
99 false),
100 FS);
101 if (!CI)
102 return llvm::make_error<llvm::StringError>(
103 "Failed to create a compiler instance for " + File,
104 llvm::inconvertibleErrorCode());
105 CI->getFrontendOpts().DisableFree = false;
106 // Parse the main file to get all existing #includes in the file, and then we
107 // can make sure the same header (even with different include path) is not
108 // added more than once.
109 CI->getPreprocessorOpts().SingleFileParseMode = true;
110
Haojian Wub603a5e2018-02-22 13:35:01 +0000111 // The diagnostic options must be set before creating a CompilerInstance.
112 CI->getDiagnosticOpts().IgnoreWarnings = true;
Eric Liuc5105f92018-02-16 14:15:55 +0000113 auto Clang = prepareCompilerInstance(
114 std::move(CI), /*Preamble=*/nullptr,
115 llvm::MemoryBuffer::getMemBuffer(Code, File),
116 std::make_shared<PCHContainerOperations>(), FS, IgnoreDiags);
Eric Liuc5105f92018-02-16 14:15:55 +0000117
118 if (Clang->getFrontendOpts().Inputs.empty())
119 return llvm::make_error<llvm::StringError>(
120 "Empty frontend action inputs empty for file " + File,
121 llvm::inconvertibleErrorCode());
122 PreprocessOnlyAction Action;
123 if (!Action.BeginSourceFile(*Clang, Clang->getFrontendOpts().Inputs[0]))
124 return llvm::make_error<llvm::StringError>(
125 "Failed to begin preprocessor only action for file " + File,
126 llvm::inconvertibleErrorCode());
Eric Liu155f5a42018-05-14 12:19:16 +0000127 std::vector<Inclusion> Inclusions;
128 Clang->getPreprocessor().addPPCallbacks(collectInclusionsInMainFileCallback(
129 Clang->getSourceManager(),
130 [&Inclusions](Inclusion Inc) { Inclusions.push_back(std::move(Inc)); }));
Eric Liuc5105f92018-02-16 14:15:55 +0000131 if (!Action.Execute())
132 return llvm::make_error<llvm::StringError>(
133 "Failed to execute preprocessor only action for file " + File,
134 llvm::inconvertibleErrorCode());
Eric Liu155f5a42018-05-14 12:19:16 +0000135 llvm::StringSet<> IncludedHeaders;
136 for (const auto &Inc : Inclusions) {
137 IncludedHeaders.insert(Inc.Written);
138 if (!Inc.Resolved.empty())
139 IncludedHeaders.insert(Inc.Resolved);
140 }
Eric Liu6c8e8582018-02-26 08:32:13 +0000141 auto Included = [&](llvm::StringRef Header) {
Eric Liu155f5a42018-05-14 12:19:16 +0000142 return IncludedHeaders.find(Header) != IncludedHeaders.end();
Eric Liu6c8e8582018-02-26 08:32:13 +0000143 };
144 if (Included(DeclaringHeader.File) || Included(InsertedHeader.File))
145 return "";
Eric Liuc5105f92018-02-16 14:15:55 +0000146
147 auto &HeaderSearchInfo = Clang->getPreprocessor().getHeaderSearchInfo();
148 bool IsSystem = false;
Eric Liu6c8e8582018-02-26 08:32:13 +0000149
150 if (InsertedHeader.Verbatim)
151 return InsertedHeader.File;
152
Eric Liuc5105f92018-02-16 14:15:55 +0000153 std::string Suggested = HeaderSearchInfo.suggestPathToFileForDiagnostics(
Eric Liu6c8e8582018-02-26 08:32:13 +0000154 InsertedHeader.File, CompileCommand.Directory, &IsSystem);
Eric Liuc5105f92018-02-16 14:15:55 +0000155 if (IsSystem)
156 Suggested = "<" + Suggested + ">";
157 else
158 Suggested = "\"" + Suggested + "\"";
159
Eric Liu6c8e8582018-02-26 08:32:13 +0000160 log("Suggested #include for " + InsertedHeader.File + " is: " + Suggested);
Eric Liuc5105f92018-02-16 14:15:55 +0000161 return Suggested;
162}
163
164} // namespace clangd
165} // namespace clang