[clang-tools-extra] f8865c0 - [clangd] Pull out a isProtoFile function.

Haojian Wu via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 5 03:08:32 PST 2020


Author: Haojian Wu
Date: 2020-02-05T12:04:03+01:00
New Revision: f8865c01944fe409857eeb30e0963bfa5ced294d

URL: https://github.com/llvm/llvm-project/commit/f8865c01944fe409857eeb30e0963bfa5ced294d
DIFF: https://github.com/llvm/llvm-project/commit/f8865c01944fe409857eeb30e0963bfa5ced294d.diff

LOG: [clangd] Pull out a isProtoFile function.

Reviewers: kadircet

Reviewed By: kadircet

Subscribers: merge_guards_bot, mgorny, ilya-biryukov, MaskRay, jkorous, arphaman, usaxena95, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D73780

Added: 
    

Modified: 
    clang-tools-extra/clangd/SourceCode.cpp
    clang-tools-extra/clangd/SourceCode.h
    clang-tools-extra/clangd/index/SymbolCollector.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/SourceCode.cpp b/clang-tools-extra/clangd/SourceCode.cpp
index 67cea92962a1..5ac738055b54 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 47fde505c252..112f217df134 100644
--- a/clang-tools-extra/clangd/SourceCode.h
+++ b/clang-tools-extra/clangd/SourceCode.h
@@ -298,6 +298,9 @@ llvm::Optional<DefinedMacro> locateMacroAt(SourceLocation Loc,
 bool isHeaderFile(llvm::StringRef FileName,
                   llvm::Optional<LangOptions> 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 48c26bae5c2c..8718b7bcd48f 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.


        


More information about the cfe-commits mailing list