]> granicus.if.org Git - clang/commitdiff
add a fixit when 'main' does ot return 'int'; review welcome
authorGabor Greif <ggreif@gmail.com>
Wed, 8 Sep 2010 00:31:13 +0000 (00:31 +0000)
committerGabor Greif <ggreif@gmail.com>
Wed, 8 Sep 2010 00:31:13 +0000 (00:31 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@113324 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaDecl.cpp
test/FixIt/fixit.cpp

index df077fd70978904d6b84701ed38b543d9baa551b..2b42fb60fbb17ed7cebbe657bfb5732a66ab498a 100644 (file)
@@ -3171,7 +3171,7 @@ Sema::ActOnFunctionDeclarator(Scope* S, Declarator& D, DeclContext* DC,
       SC = SC_Static;
     break;
   }
-  case DeclSpec::SCS_private_extern: SC = SC_PrivateExtern;break;
+  case DeclSpec::SCS_private_extern: SC = SC_PrivateExtern; break;
   }
 
   if (D.getDeclSpec().isThreadSpecified())
@@ -3976,8 +3976,14 @@ void Sema::CheckMain(FunctionDecl* FD) {
   const FunctionType* FT = T->getAs<FunctionType>();
 
   if (!Context.hasSameUnqualifiedType(FT->getResultType(), Context.IntTy)) {
-    // TODO: add a replacement fixit to turn the return type into 'int'.
-    Diag(FD->getTypeSpecStartLoc(), diag::err_main_returns_nonint);
+    TypeSourceInfo *TSI = FD->getTypeSourceInfo();
+    TypeLoc TL = TSI->getTypeLoc();
+    const SemaDiagnosticBuilder& D = Diag(FD->getTypeSpecStartLoc(),
+                                          diag::err_main_returns_nonint);
+    if (FunctionTypeLoc* PTL = dyn_cast<FunctionTypeLoc>(&TL)) {
+      D << FixItHint::CreateReplacement(PTL->getResultLoc().getSourceRange(),
+                                        "int");
+    }
     FD->setInvalidDecl(true);
   }
 
index 5a28132397006a0b97611ac93c4be50d958545e9..9b39c9e8a57e202fdaf641419a70caa9c83becb4 100644 (file)
@@ -60,3 +60,9 @@ namespace rdar7796492 {
   }
 
 }
+
+CT<1> main(void); // expected-error{{'main' must return 'int'}}
+
+// typedef CT<1> mainT(void);
+// mainT main; // TODO
+