r347529 - [CodeComplete] Simplify CodeCompleteConsumer.cpp, NFC

Fangrui Song via cfe-commits cfe-commits at lists.llvm.org
Sun Nov 25 12:57:06 PST 2018


Author: maskray
Date: Sun Nov 25 12:57:05 2018
New Revision: 347529

URL: http://llvm.org/viewvc/llvm-project?rev=347529&view=rev
Log:
[CodeComplete] Simplify CodeCompleteConsumer.cpp, NFC

Use range-based for loops
Use XStr.compare(YStr) < 0
Format misaligned code

Modified:
    cfe/trunk/lib/Sema/CodeCompleteConsumer.cpp

Modified: cfe/trunk/lib/Sema/CodeCompleteConsumer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/CodeCompleteConsumer.cpp?rev=347529&r1=347528&r2=347529&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/CodeCompleteConsumer.cpp (original)
+++ cfe/trunk/lib/Sema/CodeCompleteConsumer.cpp Sun Nov 25 12:57:05 2018
@@ -272,23 +272,18 @@ CodeCompletionString::Chunk::CreateResul
   return Chunk(CK_ResultType, ResultType);
 }
 
-CodeCompletionString::Chunk
-CodeCompletionString::Chunk::CreateCurrentParameter(
-                                                const char *CurrentParameter) {
+CodeCompletionString::Chunk CodeCompletionString::Chunk::CreateCurrentParameter(
+    const char *CurrentParameter) {
   return Chunk(CK_CurrentParameter, CurrentParameter);
 }
 
-CodeCompletionString::CodeCompletionString(const Chunk *Chunks,
-                                           unsigned NumChunks,
-                                           unsigned Priority,
-                                           CXAvailabilityKind Availability,
-                                           const char **Annotations,
-                                           unsigned NumAnnotations,
-                                           StringRef ParentName,
-                                           const char *BriefComment)
-    : NumChunks(NumChunks), NumAnnotations(NumAnnotations),
-      Priority(Priority), Availability(Availability),
-      ParentName(ParentName), BriefComment(BriefComment) {
+CodeCompletionString::CodeCompletionString(
+    const Chunk *Chunks, unsigned NumChunks, unsigned Priority,
+    CXAvailabilityKind Availability, const char **Annotations,
+    unsigned NumAnnotations, StringRef ParentName, const char *BriefComment)
+    : NumChunks(NumChunks), NumAnnotations(NumAnnotations), Priority(Priority),
+      Availability(Availability), ParentName(ParentName),
+      BriefComment(BriefComment) {
   assert(NumChunks <= 0xffff);
   assert(NumAnnotations <= 0xffff);
 
@@ -296,7 +291,8 @@ CodeCompletionString::CodeCompletionStri
   for (unsigned I = 0; I != NumChunks; ++I)
     StoredChunks[I] = Chunks[I];
 
-  const char **StoredAnnotations = reinterpret_cast<const char **>(StoredChunks + NumChunks);
+  const char **StoredAnnotations =
+      reinterpret_cast<const char **>(StoredChunks + NumChunks);
   for (unsigned I = 0; I != NumAnnotations; ++I)
     StoredAnnotations[I] = Annotations[I];
 }
@@ -307,7 +303,7 @@ unsigned CodeCompletionString::getAnnota
 
 const char *CodeCompletionString::getAnnotation(unsigned AnnotationNr) const {
   if (AnnotationNr < NumAnnotations)
-    return reinterpret_cast<const char * const*>(end())[AnnotationNr];
+    return reinterpret_cast<const char *const *>(end())[AnnotationNr];
   else
     return nullptr;
 }
@@ -316,27 +312,33 @@ std::string CodeCompletionString::getAsS
   std::string Result;
   llvm::raw_string_ostream OS(Result);
 
-  for (iterator C = begin(), CEnd = end(); C != CEnd; ++C) {
-    switch (C->Kind) {
-    case CK_Optional: OS << "{#" << C->Optional->getAsString() << "#}"; break;
-    case CK_Placeholder: OS << "<#" << C->Text << "#>"; break;
-
+  for (const Chunk &C : *this) {
+    switch (C.Kind) {
+    case CK_Optional:
+      OS << "{#" << C.Optional->getAsString() << "#}";
+      break;
+    case CK_Placeholder:
+      OS << "<#" << C.Text << "#>";
+      break;
     case CK_Informative:
     case CK_ResultType:
-      OS << "[#" << C->Text << "#]";
+      OS << "[#" << C.Text << "#]";
+      break;
+    case CK_CurrentParameter:
+      OS << "<#" << C.Text << "#>";
+      break;
+    default:
+      OS << C.Text;
       break;
-
-    case CK_CurrentParameter: OS << "<#" << C->Text << "#>"; break;
-    default: OS << C->Text; break;
     }
   }
   return OS.str();
 }
 
 const char *CodeCompletionString::getTypedText() const {
-  for (iterator C = begin(), CEnd = end(); C != CEnd; ++C)
-    if (C->Kind == CK_TypedText)
-      return C->Text;
+  for (const Chunk &C : *this)
+    if (C.Kind == CK_TypedText)
+      return C.Text;
 
   return nullptr;
 }
@@ -371,7 +373,7 @@ StringRef CodeCompletionTUInfo::getParen
   // Find the interesting names.
   SmallVector<const DeclContext *, 2> Contexts;
   while (DC && !DC->isFunctionOrMethod()) {
-    if (const NamedDecl *ND = dyn_cast<NamedDecl>(DC)) {
+    if (const auto *ND = dyn_cast<NamedDecl>(DC)) {
       if (ND->getIdentifier())
         Contexts.push_back(DC);
     }
@@ -390,11 +392,11 @@ StringRef CodeCompletionTUInfo::getParen
         OS << "::";
       }
 
-      const DeclContext *CurDC = Contexts[I-1];
-      if (const ObjCCategoryImplDecl *CatImpl = dyn_cast<ObjCCategoryImplDecl>(CurDC))
+      const DeclContext *CurDC = Contexts[I - 1];
+      if (const auto *CatImpl = dyn_cast<ObjCCategoryImplDecl>(CurDC))
         CurDC = CatImpl->getCategoryDecl();
 
-      if (const ObjCCategoryDecl *Cat = dyn_cast<ObjCCategoryDecl>(CurDC)) {
+      if (const auto *Cat = dyn_cast<ObjCCategoryDecl>(CurDC)) {
         const ObjCInterfaceDecl *Interface = Cat->getClassInterface();
         if (!Interface) {
           // Assign an empty StringRef but with non-null data to distinguish
@@ -420,11 +422,9 @@ CodeCompletionString *CodeCompletionBuil
       sizeof(CodeCompletionString) + sizeof(Chunk) * Chunks.size() +
           sizeof(const char *) * Annotations.size(),
       alignof(CodeCompletionString));
-  CodeCompletionString *Result
-    = new (Mem) CodeCompletionString(Chunks.data(), Chunks.size(),
-                                     Priority, Availability,
-                                     Annotations.data(), Annotations.size(),
-                                     ParentName, BriefComment);
+  CodeCompletionString *Result = new (Mem) CodeCompletionString(
+      Chunks.data(), Chunks.size(), Priority, Availability, Annotations.data(),
+      Annotations.size(), ParentName, BriefComment);
   Chunks.clear();
   return Result;
 }
@@ -453,8 +453,8 @@ void CodeCompletionBuilder::AddResultTyp
   Chunks.push_back(Chunk::CreateResultType(ResultType));
 }
 
-void
-CodeCompletionBuilder::AddCurrentParameterChunk(const char *CurrentParameter) {
+void CodeCompletionBuilder::AddCurrentParameterChunk(
+    const char *CurrentParameter) {
   Chunks.push_back(Chunk::CreateCurrentParameter(CurrentParameter));
 }
 
@@ -484,8 +484,7 @@ void CodeCompletionBuilder::addBriefComm
 //===----------------------------------------------------------------------===//
 // Code completion overload candidate implementation
 //===----------------------------------------------------------------------===//
-FunctionDecl *
-CodeCompleteConsumer::OverloadCandidate::getFunction() const {
+FunctionDecl *CodeCompleteConsumer::OverloadCandidate::getFunction() const {
   if (getKind() == CK_Function)
     return Function;
   else if (getKind() == CK_FunctionTemplate)
@@ -501,8 +500,9 @@ CodeCompleteConsumer::OverloadCandidate:
     return Function->getType()->getAs<FunctionType>();
 
   case CK_FunctionTemplate:
-    return FunctionTemplate->getTemplatedDecl()->getType()
-             ->getAs<FunctionType>();
+    return FunctionTemplate->getTemplatedDecl()
+        ->getType()
+        ->getAs<FunctionType>();
 
   case CK_FunctionType:
     return Type;
@@ -517,12 +517,12 @@ CodeCompleteConsumer::OverloadCandidate:
 
 CodeCompleteConsumer::~CodeCompleteConsumer() = default;
 
-bool PrintingCodeCompleteConsumer::isResultFilteredOut(StringRef Filter,
-                                                CodeCompletionResult Result) {
+bool PrintingCodeCompleteConsumer::isResultFilteredOut(
+    StringRef Filter, CodeCompletionResult Result) {
   switch (Result.Kind) {
   case CodeCompletionResult::RK_Declaration:
     return !(Result.Declaration->getIdentifier() &&
-            Result.Declaration->getIdentifier()->getName().startswith(Filter));
+             Result.Declaration->getIdentifier()->getName().startswith(Filter));
   case CodeCompletionResult::RK_Keyword:
     return !StringRef(Result.Keyword).startswith(Filter);
   case CodeCompletionResult::RK_Macro:
@@ -534,18 +534,16 @@ bool PrintingCodeCompleteConsumer::isRes
   llvm_unreachable("Unknown code completion result Kind.");
 }
 
-void
-PrintingCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &SemaRef,
-                                                 CodeCompletionContext Context,
-                                                 CodeCompletionResult *Results,
-                                                         unsigned NumResults) {
+void PrintingCodeCompleteConsumer::ProcessCodeCompleteResults(
+    Sema &SemaRef, CodeCompletionContext Context, CodeCompletionResult *Results,
+    unsigned NumResults) {
   std::stable_sort(Results, Results + NumResults);
 
   StringRef Filter = SemaRef.getPreprocessor().getCodeCompletionFilter();
 
   // Print the results.
   for (unsigned I = 0; I != NumResults; ++I) {
-    if(!Filter.empty() && isResultFilteredOut(Filter, Results[I]))
+    if (!Filter.empty() && isResultFilteredOut(Filter, Results[I]))
       continue;
     OS << "COMPLETION: ";
     switch (Results[I].Kind) {
@@ -594,19 +592,16 @@ PrintingCodeCompleteConsumer::ProcessCod
 
     case CodeCompletionResult::RK_Macro:
       OS << Results[I].Macro->getName();
-      if (CodeCompletionString *CCS
-            = Results[I].CreateCodeCompletionString(SemaRef, Context,
-                                                    getAllocator(),
-                                                    CCTUInfo,
-                                                    includeBriefComments())) {
+      if (CodeCompletionString *CCS = Results[I].CreateCodeCompletionString(
+              SemaRef, Context, getAllocator(), CCTUInfo,
+              includeBriefComments())) {
         OS << " : " << CCS->getAsString();
       }
       OS << '\n';
       break;
 
     case CodeCompletionResult::RK_Pattern:
-      OS << "Pattern : "
-         << Results[I].Pattern->getAsString() << '\n';
+      OS << "Pattern : " << Results[I].Pattern->getAsString() << '\n';
       break;
     }
   }
@@ -635,7 +630,9 @@ static std::string getOverloadAsString(c
     case CodeCompletionString::CK_Optional:
       break;
 
-    default: OS << C.Text; break;
+    default:
+      OS << C.Text;
+      break;
     }
   }
   return OS.str();
@@ -691,7 +688,7 @@ void CodeCompletionResult::computeCursor
       break;
     }
 
-    if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(Declaration))
+    if (const auto *Function = dyn_cast<FunctionDecl>(Declaration))
       if (Function->isDeleted())
         Availability = CXAvailability_NotAvailable;
 
@@ -725,15 +722,15 @@ void CodeCompletionResult::computeCursor
 /// saved into Saved and the returned StringRef will refer to it.
 StringRef CodeCompletionResult::getOrderedName(std::string &Saved) const {
   switch (Kind) {
-    case RK_Keyword:
-      return Keyword;
-    case RK_Pattern:
-      return Pattern->getTypedText();
-    case RK_Macro:
-      return Macro->getName();
-    case RK_Declaration:
-      // Handle declarations below.
-      break;
+  case RK_Keyword:
+    return Keyword;
+  case RK_Pattern:
+    return Pattern->getTypedText();
+  case RK_Macro:
+    return Macro->getName();
+  case RK_Declaration:
+    // Handle declarations below.
+    break;
   }
 
   DeclarationName Name = Declaration->getDeclName();
@@ -743,8 +740,7 @@ StringRef CodeCompletionResult::getOrder
   if (IdentifierInfo *Id = Name.getAsIdentifierInfo())
     return Id->getName();
   if (Name.isObjCZeroArgSelector())
-    if (IdentifierInfo *Id
-        = Name.getObjCSelector().getIdentifierInfoForSlot(0))
+    if (IdentifierInfo *Id = Name.getObjCSelector().getIdentifierInfoForSlot(0))
       return Id->getName();
 
   Saved = Name.getAsString();
@@ -761,9 +757,5 @@ bool clang::operator<(const CodeCompleti
     return cmp < 0;
 
   // If case-insensitive comparison fails, try case-sensitive comparison.
-  cmp = XStr.compare(YStr);
-  if (cmp)
-    return cmp < 0;
-
-  return false;
+  return XStr.compare(YStr) < 0;
 }




More information about the cfe-commits mailing list