@@ -873,19 +873,88 @@ void CompletionLookup::addVarDeclRef(const VarDecl *VD,
873873 Builder.addFlair (CodeCompletionFlairBit::ExpressionSpecific);
874874}
875875
876+ // / Return whether \p param has a non-desirable default value for code
877+ // / completion.
878+ // /
879+ // / 'ClangImporter::Implementation::inferDefaultArgument()' automatically adds
880+ // / default values for some parameters;
881+ // / * NS_OPTIONS enum type with the name '...Options'.
882+ // / * NSDictionary and labeled 'options', 'attributes', or 'userInfo'.
883+ // /
884+ // / But sometimes, this behavior isn't really desirable. This function add a
885+ // / heuristic where if a parameter matches all the following condition, we
886+ // / consider the imported default value is _not_ desirable:
887+ // / * it is the first parameter,
888+ // / * it doesn't have an argument label, and
889+ // / * the imported function base name ends with those words
890+ // / For example, ClangImporter imports:
891+ // /
892+ // / -(void)addAttributes:(NSDictionary *)attrs, options:(NSDictionary *)opts;
893+ // /
894+ // / as:
895+ // /
896+ // / func addAttributes(_ attrs: [AnyHashable:Any] = [:],
897+ // / options opts: [AnyHashable:Any] = [:])
898+ // /
899+ // / In this case, we don't want 'attrs' defaulted because the function name have
900+ // / 'Attribute' in its name so calling 'value.addAttribute()' doesn't make
901+ // / sense, but we _do_ want to keep 'opts' defaulted.
902+ // /
903+ // / Note that:
904+ // /
905+ // / -(void)performWithOptions:(NSDictionary *) opts;
906+ // /
907+ // / This doesn't match the condition because the base name of the function in
908+ // / Swift is 'peform':
909+ // /
910+ // / func perform(options opts: [AnyHashable:Any] = [:])
911+ // /
912+ bool isNonDesirableImportedDefaultArg (const ParamDecl *param) {
913+ auto kind = param->getDefaultArgumentKind ();
914+ if (kind != DefaultArgumentKind::EmptyArray &&
915+ kind != DefaultArgumentKind::EmptyDictionary)
916+ return false ;
917+
918+ if (!param->getArgumentName ().empty ())
919+ return false ;
920+
921+ auto *func = dyn_cast<FuncDecl>(param->getDeclContext ());
922+ if (!func->hasClangNode ())
923+ return false ;
924+ if (func->getParameters ()->front () != param)
925+ return false ;
926+ if (func->getBaseName ().isSpecial ())
927+ return false ;
928+
929+ auto baseName = func->getBaseName ().getIdentifier ().str ();
930+ switch (kind) {
931+ case DefaultArgumentKind::EmptyArray:
932+ return (baseName.endswith (" Options" ));
933+ case DefaultArgumentKind::EmptyDictionary:
934+ return (baseName.endswith (" Options" ) || baseName.endswith (" Attributes" ) ||
935+ baseName.endswith (" UserInfo" ));
936+ default :
937+ llvm_unreachable (" unhandled DefaultArgumentKind" );
938+ }
939+ }
940+
876941bool CompletionLookup::hasInterestingDefaultValue (const ParamDecl *param) {
877942 if (!param)
878943 return false ;
879944
880945 switch (param->getDefaultArgumentKind ()) {
881946 case DefaultArgumentKind::Normal:
882947 case DefaultArgumentKind::NilLiteral:
883- case DefaultArgumentKind::EmptyArray:
884- case DefaultArgumentKind::EmptyDictionary:
885948 case DefaultArgumentKind::StoredProperty:
886949 case DefaultArgumentKind::Inherited:
887950 return true ;
888951
952+ case DefaultArgumentKind::EmptyArray:
953+ case DefaultArgumentKind::EmptyDictionary:
954+ if (isNonDesirableImportedDefaultArg (param))
955+ return false ;
956+ return true ;
957+
889958 case DefaultArgumentKind::None:
890959#define MAGIC_IDENTIFIER (NAME, STRING, SYNTAX_KIND ) \
891960 case DefaultArgumentKind::NAME:
@@ -924,7 +993,8 @@ bool CompletionLookup::addCallArgumentPatterns(
924993 bool hasDefault = false ;
925994 if (!declParams.empty ()) {
926995 const ParamDecl *PD = declParams[i];
927- hasDefault = PD->isDefaultArgument ();
996+ hasDefault =
997+ PD->isDefaultArgument () && !isNonDesirableImportedDefaultArg (PD);
928998 // Skip default arguments if we're either not including them or they
929999 // aren't interesting
9301000 if (hasDefault &&
0 commit comments