diff --git a/clang-tools-extra/clangd/SourceCode.cpp b/clang-tools-extra/clangd/SourceCode.cpp index 67cea92962a11..5ac738055b541 100644 --- a/clang-tools-extra/clangd/SourceCode.cpp +++ b/clang-tools-extra/clangd/SourceCode.cpp @@ -1127,5 +1127,17 @@ bool isHeaderFile(llvm::StringRef FileName, return Lang != types::TY_INVALID && types::onlyPrecompileType(Lang); } +bool isProtoFile(SourceLocation Loc, const SourceManager &SM) { + auto FileName = SM.getFilename(Loc); + if (!FileName.endswith(".proto.h") && !FileName.endswith(".pb.h")) + return false; + auto FID = SM.getFileID(Loc); + // All proto generated headers should start with this line. + static const char *PROTO_HEADER_COMMENT = + "// Generated by the protocol buffer compiler. DO NOT EDIT!"; + // Double check that this is an actual protobuf header. + return SM.getBufferData(FID).startswith(PROTO_HEADER_COMMENT); +} + } // namespace clangd } // namespace clang diff --git a/clang-tools-extra/clangd/SourceCode.h b/clang-tools-extra/clangd/SourceCode.h index 47fde505c2522..112f217df1346 100644 --- a/clang-tools-extra/clangd/SourceCode.h +++ b/clang-tools-extra/clangd/SourceCode.h @@ -298,6 +298,9 @@ llvm::Optional locateMacroAt(SourceLocation Loc, bool isHeaderFile(llvm::StringRef FileName, llvm::Optional LangOpts = llvm::None); +/// Returns true if the given location is in a generated protobuf file. +bool isProtoFile(SourceLocation Loc, const SourceManager &SourceMgr); + } // namespace clangd } // namespace clang #endif diff --git a/clang-tools-extra/clangd/index/SymbolCollector.cpp b/clang-tools-extra/clangd/index/SymbolCollector.cpp index 48c26bae5c2c4..8718b7bcd48f8 100644 --- a/clang-tools-extra/clangd/index/SymbolCollector.cpp +++ b/clang-tools-extra/clangd/index/SymbolCollector.cpp @@ -70,25 +70,13 @@ std::string toURI(const SourceManager &SM, llvm::StringRef Path, return URI::create(AbsolutePath).toString(); } -// All proto generated headers should start with this line. -static const char *PROTO_HEADER_COMMENT = - "// Generated by the protocol buffer compiler. DO NOT EDIT!"; - // Checks whether the decl is a private symbol in a header generated by // protobuf compiler. -// To identify whether a proto header is actually generated by proto compiler, -// we check whether it starts with PROTO_HEADER_COMMENT. // FIXME: make filtering extensible when there are more use cases for symbol // filters. bool isPrivateProtoDecl(const NamedDecl &ND) { const auto &SM = ND.getASTContext().getSourceManager(); - auto Loc = nameLocation(ND, SM); - auto FileName = SM.getFilename(Loc); - if (!FileName.endswith(".proto.h") && !FileName.endswith(".pb.h")) - return false; - auto FID = SM.getFileID(Loc); - // Double check that this is an actual protobuf header. - if (!SM.getBufferData(FID).startswith(PROTO_HEADER_COMMENT)) + if (!isProtoFile(nameLocation(ND, SM), SM)) return false; // ND without identifier can be operators.