Skip to content

Commit 6e01718

Browse files
committed
[clangd] Store ppdirective in Inclusion
Summary: This will enable PreamblePatching proposed in D77392 craft a more informed patch. Reviewers: sammccall Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D78235
1 parent ee959dd commit 6e01718

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

clang-tools-extra/clangd/Headers.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class RecordHeaders : public PPCallbacks {
2828

2929
// Record existing #includes - both written and resolved paths. Only #includes
3030
// in the main file are collected.
31-
void InclusionDirective(SourceLocation HashLoc, const Token & /*IncludeTok*/,
31+
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
3232
llvm::StringRef FileName, bool IsAngled,
3333
CharSourceRange FilenameRange, const FileEntry *File,
3434
llvm::StringRef /*SearchPath*/,
@@ -44,6 +44,7 @@ class RecordHeaders : public PPCallbacks {
4444
Inc.Resolved = std::string(File ? File->tryGetRealPathName() : "");
4545
Inc.HashOffset = SM.getFileOffset(HashLoc);
4646
Inc.FileKind = FileKind;
47+
Inc.Directive = IncludeTok.getIdentifierInfo()->getPPKeywordID();
4748
}
4849
if (File) {
4950
auto *IncludingFileEntry = SM.getFileEntryForID(SM.getFileID(HashLoc));

clang-tools-extra/clangd/Headers.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "Protocol.h"
1414
#include "SourceCode.h"
1515
#include "index/Symbol.h"
16+
#include "clang/Basic/TokenKinds.h"
1617
#include "clang/Format/Format.h"
1718
#include "clang/Lex/HeaderSearch.h"
1819
#include "clang/Lex/PPCallbacks.h"
@@ -22,6 +23,7 @@
2223
#include "llvm/ADT/StringSet.h"
2324
#include "llvm/Support/Error.h"
2425
#include "llvm/Support/VirtualFileSystem.h"
26+
#include <string>
2527

2628
namespace clang {
2729
namespace clangd {
@@ -50,9 +52,10 @@ llvm::SmallVector<llvm::StringRef, 1> getRankedIncludes(const Symbol &Sym);
5052

5153
// An #include directive that we found in the main file.
5254
struct Inclusion {
53-
Range R; // Inclusion range.
54-
std::string Written; // Inclusion name as written e.g. <vector>.
55-
Path Resolved; // Resolved path of included file. Empty if not resolved.
55+
Range R; // Inclusion range.
56+
tok::PPKeywordKind Directive; // Directive used for inclusion, e.g. import
57+
std::string Written; // Inclusion name as written e.g. <vector>.
58+
Path Resolved; // Resolved path of included file. Empty if not resolved.
5659
unsigned HashOffset = 0; // Byte offset from start of file to #.
5760
SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User;
5861
};

clang-tools-extra/clangd/unittests/HeadersTests.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "Compiler.h"
1212
#include "TestFS.h"
1313
#include "TestTU.h"
14+
#include "clang/Basic/TokenKinds.h"
1415
#include "clang/Frontend/CompilerInstance.h"
1516
#include "clang/Frontend/CompilerInvocation.h"
1617
#include "clang/Frontend/FrontendActions.h"
@@ -127,6 +128,7 @@ class HeadersTest : public ::testing::Test {
127128
MATCHER_P(Written, Name, "") { return arg.Written == Name; }
128129
MATCHER_P(Resolved, Name, "") { return arg.Resolved == Name; }
129130
MATCHER_P(IncludeLine, N, "") { return arg.R.start.line == N; }
131+
MATCHER_P(Directive, D, "") { return arg.Directive == D; }
130132

131133
MATCHER_P2(Distance, File, D, "") {
132134
if (arg.getKey() != File)
@@ -201,6 +203,19 @@ TEST_F(HeadersTest, UnResolvedInclusion) {
201203
UnorderedElementsAre(Distance(MainFile, 0u)));
202204
}
203205

206+
TEST_F(HeadersTest, IncludeDirective) {
207+
FS.Files[MainFile] = R"cpp(
208+
#include "foo.h"
209+
#import "foo.h"
210+
#include_next "foo.h"
211+
)cpp";
212+
213+
EXPECT_THAT(collectIncludes().MainFileIncludes,
214+
UnorderedElementsAre(Directive(tok::pp_include),
215+
Directive(tok::pp_import),
216+
Directive(tok::pp_include_next)));
217+
}
218+
204219
TEST_F(HeadersTest, InsertInclude) {
205220
std::string Path = testPath("sub/bar.h");
206221
FS.Files[Path] = "";

0 commit comments

Comments
 (0)