kadircet created this revision. kadircet added a reviewer: sammccall. Herald added subscribers: cfe-commits, usaxena95, arphaman, jkorous, MaskRay, ilya-biryukov. Herald added a project: clang.
This will enable PreamblePatching proposed in D77392 <https://reviews.llvm.org/D77392> craft a more informed patch. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D78235 Files: clang-tools-extra/clangd/Headers.cpp clang-tools-extra/clangd/Headers.h clang-tools-extra/clangd/unittests/HeadersTests.cpp Index: clang-tools-extra/clangd/unittests/HeadersTests.cpp =================================================================== --- clang-tools-extra/clangd/unittests/HeadersTests.cpp +++ clang-tools-extra/clangd/unittests/HeadersTests.cpp @@ -127,6 +127,7 @@ MATCHER_P(Written, Name, "") { return arg.Written == Name; } MATCHER_P(Resolved, Name, "") { return arg.Resolved == Name; } MATCHER_P(IncludeLine, N, "") { return arg.R.start.line == N; } +MATCHER_P(Directive, D, "") { return arg.Directive == D; } MATCHER_P2(Distance, File, D, "") { if (arg.getKey() != File) @@ -201,6 +202,18 @@ UnorderedElementsAre(Distance(MainFile, 0u))); } +TEST_F(HeadersTest, IncludeDirective) { + FS.Files[MainFile] = R"cpp( +#include "foo.h" +#import "foo.h" +#include_next "foo.h" +)cpp"; + + EXPECT_THAT(collectIncludes().MainFileIncludes, + UnorderedElementsAre(Directive("include"), Directive("import"), + Directive("include_next"))); +} + TEST_F(HeadersTest, InsertInclude) { std::string Path = testPath("sub/bar.h"); FS.Files[Path] = ""; Index: clang-tools-extra/clangd/Headers.h =================================================================== --- clang-tools-extra/clangd/Headers.h +++ clang-tools-extra/clangd/Headers.h @@ -22,6 +22,7 @@ #include "llvm/ADT/StringSet.h" #include "llvm/Support/Error.h" #include "llvm/Support/VirtualFileSystem.h" +#include <string> namespace clang { namespace clangd { @@ -50,9 +51,10 @@ // An #include directive that we found in the main file. struct Inclusion { - Range R; // Inclusion range. - std::string Written; // Inclusion name as written e.g. <vector>. - Path Resolved; // Resolved path of included file. Empty if not resolved. + Range R; // Inclusion range. + std::string Directive; // Directive used for inclusion, e.g. import + std::string Written; // Inclusion name as written e.g. <vector>. + Path Resolved; // Resolved path of included file. Empty if not resolved. unsigned HashOffset = 0; // Byte offset from start of file to #. SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User; }; Index: clang-tools-extra/clangd/Headers.cpp =================================================================== --- clang-tools-extra/clangd/Headers.cpp +++ clang-tools-extra/clangd/Headers.cpp @@ -28,7 +28,7 @@ // Record existing #includes - both written and resolved paths. Only #includes // in the main file are collected. - void InclusionDirective(SourceLocation HashLoc, const Token & /*IncludeTok*/, + void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, llvm::StringRef FileName, bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File, llvm::StringRef /*SearchPath*/, @@ -44,6 +44,7 @@ Inc.Resolved = std::string(File ? File->tryGetRealPathName() : ""); Inc.HashOffset = SM.getFileOffset(HashLoc); Inc.FileKind = FileKind; + Inc.Directive = IncludeTok.getIdentifierInfo()->getName().str(); } if (File) { auto *IncludingFileEntry = SM.getFileEntryForID(SM.getFileID(HashLoc));
Index: clang-tools-extra/clangd/unittests/HeadersTests.cpp =================================================================== --- clang-tools-extra/clangd/unittests/HeadersTests.cpp +++ clang-tools-extra/clangd/unittests/HeadersTests.cpp @@ -127,6 +127,7 @@ MATCHER_P(Written, Name, "") { return arg.Written == Name; } MATCHER_P(Resolved, Name, "") { return arg.Resolved == Name; } MATCHER_P(IncludeLine, N, "") { return arg.R.start.line == N; } +MATCHER_P(Directive, D, "") { return arg.Directive == D; } MATCHER_P2(Distance, File, D, "") { if (arg.getKey() != File) @@ -201,6 +202,18 @@ UnorderedElementsAre(Distance(MainFile, 0u))); } +TEST_F(HeadersTest, IncludeDirective) { + FS.Files[MainFile] = R"cpp( +#include "foo.h" +#import "foo.h" +#include_next "foo.h" +)cpp"; + + EXPECT_THAT(collectIncludes().MainFileIncludes, + UnorderedElementsAre(Directive("include"), Directive("import"), + Directive("include_next"))); +} + TEST_F(HeadersTest, InsertInclude) { std::string Path = testPath("sub/bar.h"); FS.Files[Path] = ""; Index: clang-tools-extra/clangd/Headers.h =================================================================== --- clang-tools-extra/clangd/Headers.h +++ clang-tools-extra/clangd/Headers.h @@ -22,6 +22,7 @@ #include "llvm/ADT/StringSet.h" #include "llvm/Support/Error.h" #include "llvm/Support/VirtualFileSystem.h" +#include <string> namespace clang { namespace clangd { @@ -50,9 +51,10 @@ // An #include directive that we found in the main file. struct Inclusion { - Range R; // Inclusion range. - std::string Written; // Inclusion name as written e.g. <vector>. - Path Resolved; // Resolved path of included file. Empty if not resolved. + Range R; // Inclusion range. + std::string Directive; // Directive used for inclusion, e.g. import + std::string Written; // Inclusion name as written e.g. <vector>. + Path Resolved; // Resolved path of included file. Empty if not resolved. unsigned HashOffset = 0; // Byte offset from start of file to #. SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User; }; Index: clang-tools-extra/clangd/Headers.cpp =================================================================== --- clang-tools-extra/clangd/Headers.cpp +++ clang-tools-extra/clangd/Headers.cpp @@ -28,7 +28,7 @@ // Record existing #includes - both written and resolved paths. Only #includes // in the main file are collected. - void InclusionDirective(SourceLocation HashLoc, const Token & /*IncludeTok*/, + void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, llvm::StringRef FileName, bool IsAngled, CharSourceRange FilenameRange, const FileEntry *File, llvm::StringRef /*SearchPath*/, @@ -44,6 +44,7 @@ Inc.Resolved = std::string(File ? File->tryGetRealPathName() : ""); Inc.HashOffset = SM.getFileOffset(HashLoc); Inc.FileKind = FileKind; + Inc.Directive = IncludeTok.getIdentifierInfo()->getName().str(); } if (File) { auto *IncludingFileEntry = SM.getFileEntryForID(SM.getFileID(HashLoc));
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits