[Modules] Fix a crash-on-invalid with overloaded functions
authorBruno Cardoso Lopes <bruno.cardoso@gmail.com>
Wed, 26 Apr 2017 20:13:45 +0000 (20:13 +0000)
committerBruno Cardoso Lopes <bruno.cardoso@gmail.com>
Wed, 26 Apr 2017 20:13:45 +0000 (20:13 +0000)
Do not add an overload if the function doesn't have a prototype; this
can happen if, for instance, a misplaced/malformed call site is
considered like a declaration for recovery purposes.

rdar://problem/31306325

llvm-svn: 301453

clang/lib/Sema/SemaOverload.cpp
clang/test/Modules/Inputs/malformed-overload/X.h [new file with mode: 0644]
clang/test/Modules/Inputs/malformed-overload/module.modulemap [new file with mode: 0644]
clang/test/Modules/malformed-overload.m [new file with mode: 0644]

index 29ba344..782c377 100644 (file)
@@ -11426,6 +11426,10 @@ static void AddOverloadedCallCandidate(Sema &S,
       assert(!KnownValid && "Explicit template arguments?");
       return;
     }
+    // Prevent ill-formed function decls to be added as overload candidates.
+    if (!dyn_cast<FunctionProtoType>(Func->getType()->getAs<FunctionType>()))
+      return;
+
     S.AddOverloadCandidate(Func, FoundDecl, Args, CandidateSet,
                            /*SuppressUsedConversions=*/false,
                            PartialOverloading);
diff --git a/clang/test/Modules/Inputs/malformed-overload/X.h b/clang/test/Modules/Inputs/malformed-overload/X.h
new file mode 100644 (file)
index 0000000..b659406
--- /dev/null
@@ -0,0 +1,2 @@
+@class NSString;
+extern void NSLog(NSString *format, ...) __attribute__((format(__NSString__, 1, 2))) __attribute__((not_tail_called));
diff --git a/clang/test/Modules/Inputs/malformed-overload/module.modulemap b/clang/test/Modules/Inputs/malformed-overload/module.modulemap
new file mode 100644 (file)
index 0000000..8fe4c92
--- /dev/null
@@ -0,0 +1,4 @@
+module X {
+  header "X.h"
+  export *
+}
diff --git a/clang/test/Modules/malformed-overload.m b/clang/test/Modules/malformed-overload.m
new file mode 100644 (file)
index 0000000..ad6db8a
--- /dev/null
@@ -0,0 +1,8 @@
+// RUN: %clang_cc1 -fsyntax-only -I%S/Inputs/malformed-overload -fmodules -fimplicit-module-maps -fmodules-cache-path=tmp -verify %s
+NSLog(@"%@", path); // expected-error {{expected parameter declarator}} expected-error {{expected ')'}} expected-warning {{type specifier missing}} expected-warning {{incompatible redeclaration}} expected-note {{to match this '('}} expected-note {{'NSLog' is a builtin with type}}
+#import "X.h"
+
+@class NSString;
+void f(NSString *a) {
+ NSLog(@"***** failed to get URL for %@", a);
+}