sammccall updated this revision to Diff 149732.
sammccall added a comment.

Forgot to add to debug string.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D47707

Files:
  clangd/Quality.cpp
  clangd/Quality.h
  unittests/clangd/QualityTests.cpp

Index: unittests/clangd/QualityTests.cpp
===================================================================
--- unittests/clangd/QualityTests.cpp
+++ unittests/clangd/QualityTests.cpp
@@ -28,25 +28,27 @@
 
 TEST(QualityTests, SymbolQualitySignalExtraction) {
   auto Header = TestTU::withHeaderCode(R"cpp(
-    int x;
+    int _X;
 
     [[deprecated]]
     int f() { return x; }
   )cpp");
   auto Symbols = Header.headerSymbols();
   auto AST = Header.build();
 
   SymbolQualitySignals Quality;
-  Quality.merge(findSymbol(Symbols, "x"));
+  Quality.merge(findSymbol(Symbols, "_X"));
   EXPECT_FALSE(Quality.Deprecated);
+  EXPECT_TRUE(Quality.ReservedName);
   EXPECT_EQ(Quality.SemaCCPriority, SymbolQualitySignals().SemaCCPriority);
   EXPECT_EQ(Quality.References, SymbolQualitySignals().References);
 
   Symbol F = findSymbol(Symbols, "f");
   F.References = 24; // TestTU doesn't count references, so fake it.
   Quality = {};
   Quality.merge(F);
   EXPECT_FALSE(Quality.Deprecated); // FIXME: Include deprecated bit in index.
+  EXPECT_FALSE(Quality.ReservedName);
   EXPECT_EQ(Quality.SemaCCPriority, SymbolQualitySignals().SemaCCPriority);
   EXPECT_EQ(Quality.References, 24u);
 
@@ -80,6 +82,10 @@
   Deprecated.Deprecated = true;
   EXPECT_LT(Deprecated.evaluate(), Default.evaluate());
 
+  SymbolQualitySignals ReservedName;
+  ReservedName.ReservedName = true;
+  EXPECT_LT(ReservedName.evaluate(), Default.evaluate());
+
   SymbolQualitySignals WithReferences, ManyReferences;
   WithReferences.References = 10;
   ManyReferences.References = 1000;
Index: clangd/Quality.h
===================================================================
--- clangd/Quality.h
+++ clangd/Quality.h
@@ -48,6 +48,7 @@
                                // FIXME: this is actually a mix of symbol
                                //        quality and relevance. Untangle this.
   bool Deprecated = false;
+  bool ReservedName = false; // __foo, _Foo are usually implementation details.
   unsigned References = 0;
 
   void merge(const CodeCompletionResult &SemaCCResult);
Index: clangd/Quality.cpp
===================================================================
--- clangd/Quality.cpp
+++ clangd/Quality.cpp
@@ -8,24 +8,42 @@
 //===---------------------------------------------------------------------===//
 #include "Quality.h"
 #include "index/Index.h"
+#include "clang/Basic/CharInfo.h"
 #include "clang/Sema/CodeCompleteConsumer.h"
 #include "llvm/Support/FormatVariadic.h"
 #include "llvm/Support/MathExtras.h"
 #include "llvm/Support/raw_ostream.h"
 
 namespace clang {
 namespace clangd {
 using namespace llvm;
+static bool IsReserved(StringRef Name) {
+  return Name.size() >= 2 && Name[0] == '_' &&
+         (isUppercase(Name[1]) || Name[1] == '_');
+}
 
 void SymbolQualitySignals::merge(const CodeCompletionResult &SemaCCResult) {
   SemaCCPriority = SemaCCResult.Priority;
 
   if (SemaCCResult.Availability == CXAvailability_Deprecated)
     Deprecated = true;
+
+  switch (SemaCCResult.Kind) {
+  case CodeCompletionResult::RK_Declaration:
+    if (auto *ID = SemaCCResult.Declaration->getIdentifier())
+      ReservedName = ReservedName || IsReserved(ID->getName());
+    break;
+  case CodeCompletionResult::RK_Macro:
+    ReservedName = ReservedName || IsReserved(SemaCCResult.Macro->getName());
+    break;
+  default:
+    break;
+  }
 }
 
 void SymbolQualitySignals::merge(const Symbol &IndexResult) {
   References = std::max(IndexResult.References, References);
+  ReservedName = ReservedName || IsReserved(IndexResult.Name);
 }
 
 float SymbolQualitySignals::evaluate() const {
@@ -43,6 +61,8 @@
 
   if (Deprecated)
     Score *= 0.1f;
+  if (ReservedName)
+    Score *= 0.1f;
 
   return Score;
 }
@@ -53,6 +73,7 @@
     OS << formatv("\tSemaCCPriority: {0}\n", S.SemaCCPriority);
   OS << formatv("\tReferences: {0}\n", S.References);
   OS << formatv("\tDeprecated: {0}\n", S.Deprecated);
+  OS << formatv("\tReserved name: {0}\n", S.ReservedName);
   return OS;
 }
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to