]> granicus.if.org Git - clang/commitdiff
Implement a fixit for -Wmain-return-type
authorDmitri Gribenko <gribozavr@gmail.com>
Thu, 17 Jan 2013 00:26:13 +0000 (00:26 +0000)
committerDmitri Gribenko <gribozavr@gmail.com>
Thu, 17 Jan 2013 00:26:13 +0000 (00:26 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@172684 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaDecl.cpp
test/Sema/gnu89.c
test/Sema/warn-main-return-type.c [new file with mode: 0644]

index cd009195872df81fb7bcb6c6b6f44d9c7171aec2..95e40ecda1299ac7dfeca5abc088cd58487f2796 100644 (file)
@@ -392,6 +392,7 @@ def err_main_template_decl : Error<"'main' cannot be a template">;
 def err_main_returns_nonint : Error<"'main' must return 'int'">;
 def ext_main_returns_nonint : ExtWarn<"return type of 'main' is not 'int'">,
     InGroup<MainReturnType>;
+def note_main_change_return_type : Note<"change return type to 'int'">;
 def err_main_surplus_args : Error<"too many parameters (%0) for 'main': "
     "must be 0, 2, or 3">;
 def warn_main_one_arg : Warning<"only one parameter on 'main' declaration">,
index b85e94bce8fd7cd9d7735aa8ffe7f01b4c79e4da..5ddc26ab25774d57be105be8839457a2e51e339e 100644 (file)
@@ -6397,6 +6397,23 @@ bool Sema::CheckFunctionDeclaration(Scope *S, FunctionDecl *NewFD,
   return Redeclaration;
 }
 
+static SourceRange getResultSourceRange(const FunctionDecl *FD) {
+  const TypeSourceInfo *TSI = FD->getTypeSourceInfo();
+  if (!TSI)
+    return SourceRange();
+
+  TypeLoc TL = TSI->getTypeLoc();
+  FunctionTypeLoc *FunctionTL = dyn_cast<FunctionTypeLoc>(&TL);
+  if (!FunctionTL)
+    return SourceRange();
+
+  TypeLoc ResultTL = FunctionTL->getResultLoc();
+  if (isa<BuiltinTypeLoc>(ResultTL.getUnqualifiedLoc()))
+    return ResultTL.getSourceRange();
+
+  return SourceRange();
+}
+
 void Sema::CheckMain(FunctionDecl* FD, const DeclSpec& DS) {
   // C++11 [basic.start.main]p3:  A program that declares main to be inline,
   //   static or constexpr is ill-formed.
@@ -6433,9 +6450,20 @@ void Sema::CheckMain(FunctionDecl* FD, const DeclSpec& DS) {
   } else if (getLangOpts().GNUMode && !getLangOpts().CPlusPlus) {
     Diag(FD->getTypeSpecStartLoc(), diag::ext_main_returns_nonint);
 
+    SourceRange ResultRange = getResultSourceRange(FD);
+    if (ResultRange.isValid())
+      Diag(ResultRange.getBegin(), diag::note_main_change_return_type)
+          << FixItHint::CreateReplacement(ResultRange, "int");
+
   // Otherwise, this is just a flat-out error.
   } else {
-    Diag(FD->getTypeSpecStartLoc(), diag::err_main_returns_nonint);
+    SourceRange ResultRange = getResultSourceRange(FD);
+    if (ResultRange.isValid())
+      Diag(FD->getTypeSpecStartLoc(), diag::err_main_returns_nonint)
+          << FixItHint::CreateReplacement(ResultRange, "int");
+    else
+      Diag(FD->getTypeSpecStartLoc(), diag::err_main_returns_nonint);
+
     FD->setInvalidDecl(true);
   }
 
index 189e6b0097a9376cbd1810c92273e5a5f94a5976..1b7f10fee9c23276e82bb614ea35dd69f618a0b9 100644 (file)
@@ -2,4 +2,4 @@
 
 int f(int restrict);
 
-void main() {} // expected-warning {{return type of 'main' is not 'int'}}
+void main() {} // expected-warning {{return type of 'main' is not 'int'}} expected-note {{change return type to 'int'}}
diff --git a/test/Sema/warn-main-return-type.c b/test/Sema/warn-main-return-type.c
new file mode 100644 (file)
index 0000000..bd7c59f
--- /dev/null
@@ -0,0 +1,49 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -fsyntax-only -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s
+// RUN: %clang_cc1 -fsyntax-only -fdiagnostics-parseable-fixits -x c++ %s 2>&1 | FileCheck %s
+
+// expected-note@+1 5{{previous definition is here}}
+int main() {
+  return 0;
+}
+
+// expected-error@+3 {{conflicting types for 'main}}
+// expected-warning@+2 {{return type of 'main' is not 'int'}}
+// expected-note@+1 {{change return type to 'int'}}
+void main() {
+// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:1-[[@LINE-1]]:5}:"int"
+}
+
+// expected-error@+3 {{conflicting types for 'main}}
+// expected-warning@+2 {{return type of 'main' is not 'int'}}
+// expected-note@+1 {{change return type to 'int'}}
+double main() {
+// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:1-[[@LINE-1]]:7}:"int"
+  return 0.0;
+}
+
+// Currently we suggest to replace only 'float' here because we don't store
+// enough source locations.
+//
+// expected-error@+3 {{conflicting types for 'main}}
+// expected-warning@+2 {{return type of 'main' is not 'int'}}
+// expected-note@+1 {{change return type to 'int'}}
+const float main() {
+// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:7-[[@LINE-1]]:12}:"int"
+  return 0.0f;
+}
+
+typedef void *(*fptr)(int a);
+
+// expected-error@+2 {{conflicting types for 'main}}
+// expected-warning@+1 {{return type of 'main' is not 'int'}}
+fptr main() {
+  return (fptr) 0;
+}
+
+// expected-error@+2 {{conflicting types for 'main}}
+// expected-warning@+1 {{return type of 'main' is not 'int'}}
+void *(*main())(int a) {
+  return (fptr) 0;
+}
+