diff --git a/clang-tools-extra/clangd/Headers.h b/clang-tools-extra/clangd/Headers.h --- a/clang-tools-extra/clangd/Headers.h +++ b/clang-tools-extra/clangd/Headers.h @@ -62,8 +62,15 @@ // dependencies. Doesn't own the strings it references (IncludeGraph is // self-contained). struct IncludeGraphNode { - // True if current file is a main file rather than a header. - bool IsTU = false; + enum class SourceFlag : uint8_t { + None = 0, + // Whether current file is a main file rather than a header. + IsTU = 1 << 0, + // Whether current file had any uncompilable errors during indexing. + HadErrors = 1 << 1, + }; + + SourceFlag Flags = SourceFlag::None; llvm::StringRef URI; FileDigest Digest{{0}}; std::vector DirectIncludes; @@ -74,6 +81,22 @@ // edges and multi edges. using IncludeGraph = llvm::StringMap; +inline IncludeGraphNode::SourceFlag operator|(IncludeGraphNode::SourceFlag A, + IncludeGraphNode::SourceFlag B) { + return static_cast(static_cast(A) | + static_cast(B)); +} + +inline bool operator&(IncludeGraphNode::SourceFlag A, + IncludeGraphNode::SourceFlag B) { + return static_cast(A) & static_cast(B); +} + +inline IncludeGraphNode::SourceFlag & +operator|=(IncludeGraphNode::SourceFlag &A, IncludeGraphNode::SourceFlag B) { + return A = A | B; +} + // Information captured about the inclusion graph in a translation unit. // This includes detailed information about the direct #includes, and summary // information about all transitive includes. diff --git a/clang-tools-extra/clangd/SourceCode.h b/clang-tools-extra/clangd/SourceCode.h --- a/clang-tools-extra/clangd/SourceCode.h +++ b/clang-tools-extra/clangd/SourceCode.h @@ -20,8 +20,8 @@ #include "clang/Basic/SourceManager.h" #include "clang/Format/Format.h" #include "clang/Tooling/Core/Replacement.h" -#include "llvm/ADT/StringSet.h" #include "llvm/ADT/StringRef.h" +#include "llvm/ADT/StringSet.h" #include "llvm/Support/SHA1.h" namespace clang { diff --git a/clang-tools-extra/clangd/index/Background.cpp b/clang-tools-extra/clangd/index/Background.cpp --- a/clang-tools-extra/clangd/index/Background.cpp +++ b/clang-tools-extra/clangd/index/Background.cpp @@ -297,7 +297,7 @@ // we don't even know what absolute path they should fall in. // FIXME: Also store contents from other files whenever the current contents // for those files are missing or if they had errors before. - if (HadErrors && !IGN.IsTU) + if (HadErrors && !(IGN.Flags & IncludeGraphNode::SourceFlag::IsTU)) continue; const auto AbsPath = URICache.resolve(IGN.URI); const auto DigestIt = DigestsSnapshot.find(AbsPath); @@ -581,7 +581,8 @@ SI.AbsolutePath = CurDependency.Path; SI.Shard = std::move(Shard); SI.Digest = I.getValue().Digest; - SI.CountReferences = I.getValue().IsTU; + SI.CountReferences = + I.getValue().Flags & IncludeGraphNode::SourceFlag::IsTU; IntermediateSymbols.push_back(std::move(SI)); // Check if the source needs re-indexing. // Get the digest, skip it if file doesn't exist. diff --git a/clang-tools-extra/clangd/index/IndexAction.cpp b/clang-tools-extra/clangd/index/IndexAction.cpp --- a/clang-tools-extra/clangd/index/IndexAction.cpp +++ b/clang-tools-extra/clangd/index/IndexAction.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "IndexAction.h" +#include "Headers.h" #include "Logger.h" #include "index/Relation.h" #include "index/SymbolOrigin.h" @@ -67,7 +68,8 @@ } if (auto Digest = digestFile(SM, FileID)) Node.Digest = std::move(*Digest); - Node.IsTU = FileID == SM.getMainFileID(); + if (FileID == SM.getMainFileID()) + Node.Flags |= IncludeGraphNode::SourceFlag::IsTU; Node.URI = I->getKey(); } diff --git a/clang-tools-extra/clangd/index/Serialization.cpp b/clang-tools-extra/clangd/index/Serialization.cpp --- a/clang-tools-extra/clangd/index/Serialization.cpp +++ b/clang-tools-extra/clangd/index/Serialization.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "Serialization.h" +#include "Headers.h" #include "Logger.h" #include "RIFF.h" #include "SymbolLocation.h" @@ -278,7 +279,7 @@ IncludeGraphNode readIncludeGraphNode(Reader &Data, llvm::ArrayRef Strings) { IncludeGraphNode IGN; - IGN.IsTU = Data.consume8(); + IGN.Flags = static_cast(Data.consume8()); IGN.URI = Data.consumeString(Strings); llvm::StringRef Digest = Data.consume(IGN.Digest.size()); std::copy(Digest.bytes_begin(), Digest.bytes_end(), IGN.Digest.begin()); @@ -291,7 +292,7 @@ void writeIncludeGraphNode(const IncludeGraphNode &IGN, const StringTableOut &Strings, llvm::raw_ostream &OS) { - OS.write(IGN.IsTU); + OS.write(static_cast(IGN.Flags)); writeVar(Strings.index(IGN.URI), OS); llvm::StringRef Hash(reinterpret_cast(IGN.Digest.data()), IGN.Digest.size()); @@ -443,7 +444,7 @@ // The current versioning scheme is simple - non-current versions are rejected. // If you make a breaking change, bump this version number to invalidate stored // data. Later we may want to support some backward compatibility. -constexpr static uint32_t Version = 10; +constexpr static uint32_t Version = 11; llvm::Expected readRIFF(llvm::StringRef Data) { auto RIFF = riff::readFile(Data); diff --git a/clang-tools-extra/clangd/unittests/BackgroundIndexTests.cpp b/clang-tools-extra/clangd/unittests/BackgroundIndexTests.cpp --- a/clang-tools-extra/clangd/unittests/BackgroundIndexTests.cpp +++ b/clang-tools-extra/clangd/unittests/BackgroundIndexTests.cpp @@ -31,8 +31,8 @@ } // URI cannot be empty since it references keys in the IncludeGraph. MATCHER(EmptyIncludeNode, "") { - return !arg.IsTU && !arg.URI.empty() && arg.Digest == FileDigest{{0}} && - arg.DirectIncludes.empty(); + return arg.Flags == IncludeGraphNode::SourceFlag::None && !arg.URI.empty() && + arg.Digest == FileDigest{{0}} && arg.DirectIncludes.empty(); } MATCHER_P(NumReferences, N, "") { return arg.References == N; } diff --git a/clang-tools-extra/clangd/unittests/IndexActionTests.cpp b/clang-tools-extra/clangd/unittests/IndexActionTests.cpp --- a/clang-tools-extra/clangd/unittests/IndexActionTests.cpp +++ b/clang-tools-extra/clangd/unittests/IndexActionTests.cpp @@ -6,6 +6,7 @@ // //===----------------------------------------------------------------------===// +#include "Headers.h" #include "TestFS.h" #include "index/IndexAction.h" #include "clang/Tooling/Tooling.h" @@ -25,7 +26,7 @@ std::string toUri(llvm::StringRef Path) { return URI::create(Path).toString(); } -MATCHER(IsTU, "") { return arg.IsTU; } +MATCHER(IsTU, "") { return arg.Flags & IncludeGraphNode::SourceFlag::IsTU; } MATCHER_P(HasDigest, Digest, "") { return arg.Digest == Digest; } diff --git a/clang-tools-extra/clangd/unittests/SerializationTests.cpp b/clang-tools-extra/clangd/unittests/SerializationTests.cpp --- a/clang-tools-extra/clangd/unittests/SerializationTests.cpp +++ b/clang-tools-extra/clangd/unittests/SerializationTests.cpp @@ -6,6 +6,7 @@ // //===----------------------------------------------------------------------===// +#include "Headers.h" #include "index/Index.h" #include "index/Serialization.h" #include "clang/Tooling/CompilationDatabase.h" @@ -212,7 +213,8 @@ TestContent.size()}); IGN.DirectIncludes = {"inc1", "inc2"}; IGN.URI = "URI"; - IGN.IsTU = true; + IGN.Flags |= IncludeGraphNode::SourceFlag::IsTU; + IGN.Flags |= IncludeGraphNode::SourceFlag::HadErrors; IncludeGraph Sources; Sources[IGN.URI] = IGN; // Write to binary format, and parse again. @@ -237,7 +239,7 @@ EXPECT_EQ(IGNDeserialized.Digest, IGN.Digest); EXPECT_EQ(IGNDeserialized.DirectIncludes, IGN.DirectIncludes); EXPECT_EQ(IGNDeserialized.URI, IGN.URI); - EXPECT_EQ(IGNDeserialized.IsTU, IGN.IsTU); + EXPECT_EQ(IGNDeserialized.Flags, IGN.Flags); } }