From: Douglas Gregor Date: Mon, 20 Sep 2010 21:11:48 +0000 (+0000) Subject: Tweak priorities for some types and macros: X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b05496dbd63f03bf474dae2c4d1e2142608780cc;p=clang Tweak priorities for some types and macros: - In Objective-C, we prefer BOOL to bool for historic reasons; slightly penalize "bool". - Treat Nil macro as a NULL pointer constant. - Treat YES, NO, true, and false macros as constants. - Treat the bool macro as a type. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@114356 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Sema/CodeCompleteConsumer.h b/include/clang/Sema/CodeCompleteConsumer.h index c251cedaf3..7da0fc77e9 100644 --- a/include/clang/Sema/CodeCompleteConsumer.h +++ b/include/clang/Sema/CodeCompleteConsumer.h @@ -81,7 +81,11 @@ enum { /// \brief The selector of the given message exactly matches the selector /// of the current method, which might imply that some kind of delegation /// is occurring. - CCD_SelectorMatch = -3 + CCD_SelectorMatch = -3, + + /// \brief Adjustment to the "bool" type in Objective-C, where the typedef + /// "BOOL" is preferred. + CCD_bool_in_ObjC = 1 }; /// \brief Priority value factors by which we will divide or multiply the @@ -122,9 +126,12 @@ QualType getDeclUsageType(ASTContext &C, NamedDecl *ND); /// /// \param MacroName The name of the macro. /// +/// \param LangOpts Options describing the current language dialect. +/// /// \param PreferredTypeIsPointer Whether the preferred type for the context /// of this macro is a pointer type. unsigned getMacroUsagePriority(llvm::StringRef MacroName, + const LangOptions &LangOpts, bool PreferredTypeIsPointer = false); /// \brief Determine the libclang cursor kind associated with the given diff --git a/lib/Frontend/ASTUnit.cpp b/lib/Frontend/ASTUnit.cpp index 926e017f16..4980f2dcc3 100644 --- a/lib/Frontend/ASTUnit.cpp +++ b/lib/Frontend/ASTUnit.cpp @@ -1666,6 +1666,7 @@ void AugmentedCodeCompleteConsumer::ProcessCodeCompleteResults(Sema &S, if (!Context.getPreferredType().isNull()) { if (C->Kind == CXCursor_MacroDefinition) { Priority = getMacroUsagePriority(C->Completion->getTypedText(), + S.getLangOptions(), Context.getPreferredType()->isAnyPointerType()); } else if (C->Type) { CanQualType Expected diff --git a/lib/Sema/SemaCodeComplete.cpp b/lib/Sema/SemaCodeComplete.cpp index 371921ca11..b02f2dd217 100644 --- a/lib/Sema/SemaCodeComplete.cpp +++ b/lib/Sema/SemaCodeComplete.cpp @@ -1087,7 +1087,8 @@ static void AddTypeSpecifierResults(const LangOptions &LangOpts, if (LangOpts.CPlusPlus) { // C++-specific - Results.AddResult(Result("bool", CCP_Type)); + Results.AddResult(Result("bool", CCP_Type + + (LangOpts.ObjC1? CCD_bool_in_ObjC : 0))); Results.AddResult(Result("class", CCP_Type)); Results.AddResult(Result("wchar_t", CCP_Type)); @@ -2308,15 +2309,25 @@ CodeCompleteConsumer::OverloadCandidate::CreateSignatureString( } unsigned clang::getMacroUsagePriority(llvm::StringRef MacroName, + const LangOptions &LangOpts, bool PreferredTypeIsPointer) { unsigned Priority = CCP_Macro; - // Treat the "nil" and "NULL" macros as null pointer constants. - if (MacroName.equals("nil") || MacroName.equals("NULL")) { + // Treat the "nil", "Nil" and "NULL" macros as null pointer constants. + if (MacroName.equals("nil") || MacroName.equals("NULL") || + MacroName.equals("Nil")) { Priority = CCP_Constant; if (PreferredTypeIsPointer) Priority = Priority / CCF_SimilarTypeMatch; - } + } + // Treat "YES", "NO", "true", and "false" as constants. + else if (MacroName.equals("YES") || MacroName.equals("NO") || + MacroName.equals("true") || MacroName.equals("false")) + Priority = CCP_Constant; + // Treat "bool" as a type. + else if (MacroName.equals("bool")) + Priority = CCP_Type + (LangOpts.ObjC1? CCD_bool_in_ObjC : 0); + return Priority; } @@ -2394,6 +2405,7 @@ static void AddMacroResults(Preprocessor &PP, ResultBuilder &Results, M != MEnd; ++M) { Results.AddResult(Result(M->first, getMacroUsagePriority(M->first->getName(), + PP.getLangOptions(), TargetTypeIsPointer))); } Results.ExitScope(); diff --git a/test/Index/complete-exprs.m b/test/Index/complete-exprs.m index 0171803ffe..57b6e47da5 100644 --- a/test/Index/complete-exprs.m +++ b/test/Index/complete-exprs.m @@ -1,3 +1,7 @@ +typedef signed char BOOL; +#define YES ((BOOL)1) +#define NO ((BOOL)0) +#define bool _Bool @interface A - (int)method:(id)param1; @@ -10,6 +14,10 @@ } @end -// RUN: c-index-test -code-completion-at=%s:9:2 %s | FileCheck -check-prefix=CHECK-CC1 %s +// RUN: c-index-test -code-completion-at=%s:13:2 %s | FileCheck -check-prefix=CHECK-CC1 %s // CHECK-CC1: NotImplemented:{ResultType SEL}{TypedText _cmd} (80) +// CHECK-CC1: TypedefDecl:{TypedText BOOL} (60) +// CHECK-CC1: macro definition:{TypedText bool} (61) +// CHECK-CC1: macro definition:{TypedText NO} (65) // CHECK-CC1: NotImplemented:{ResultType A *}{TypedText self} (8) +// CHECK-CC1: macro definition:{TypedText YES} (65)