[cfe-commits] r172684 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaDecl.cpp test/Sema/gnu89.c test/Sema/warn-main-return-type.c

David Blaikie dblaikie at gmail.com
Wed Jan 16 17:23:43 PST 2013


On Wed, Jan 16, 2013 at 4:26 PM, Dmitri Gribenko <gribozavr at gmail.com> wrote:
> Author: gribozavr
> Date: Wed Jan 16 18:26:13 2013
> New Revision: 172684
>
> URL: http://llvm.org/viewvc/llvm-project?rev=172684&view=rev
> Log:
> Implement a fixit for -Wmain-return-type

Addresses one part of PR8104, for what it's worth.

>
> Added:
>     cfe/trunk/test/Sema/warn-main-return-type.c
> Modified:
>     cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
>     cfe/trunk/lib/Sema/SemaDecl.cpp
>     cfe/trunk/test/Sema/gnu89.c
>
> Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=172684&r1=172683&r2=172684&view=diff
> ==============================================================================
> --- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
> +++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Wed Jan 16 18:26:13 2013
> @@ -392,6 +392,7 @@
>  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">,
>
> Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=172684&r1=172683&r2=172684&view=diff
> ==============================================================================
> --- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
> +++ cfe/trunk/lib/Sema/SemaDecl.cpp Wed Jan 16 18:26:13 2013
> @@ -6397,6 +6397,23 @@
>    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 @@
>    } 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);
>    }
>
>
> Modified: cfe/trunk/test/Sema/gnu89.c
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/gnu89.c?rev=172684&r1=172683&r2=172684&view=diff
> ==============================================================================
> --- cfe/trunk/test/Sema/gnu89.c (original)
> +++ cfe/trunk/test/Sema/gnu89.c Wed Jan 16 18:26:13 2013
> @@ -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'}}
>
> Added: cfe/trunk/test/Sema/warn-main-return-type.c
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/warn-main-return-type.c?rev=172684&view=auto
> ==============================================================================
> --- cfe/trunk/test/Sema/warn-main-return-type.c (added)
> +++ cfe/trunk/test/Sema/warn-main-return-type.c Wed Jan 16 18:26:13 2013
> @@ -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 at +1 5{{previous definition is here}}
> +int main() {
> +  return 0;
> +}
> +
> +// expected-error at +3 {{conflicting types for 'main}}
> +// expected-warning at +2 {{return type of 'main' is not 'int'}}
> +// expected-note at +1 {{change return type to 'int'}}
> +void main() {
> +// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:1-[[@LINE-1]]:5}:"int"
> +}
> +
> +// expected-error at +3 {{conflicting types for 'main}}
> +// expected-warning at +2 {{return type of 'main' is not 'int'}}
> +// expected-note at +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 at +3 {{conflicting types for 'main}}
> +// expected-warning at +2 {{return type of 'main' is not 'int'}}
> +// expected-note at +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 at +2 {{conflicting types for 'main}}
> +// expected-warning at +1 {{return type of 'main' is not 'int'}}
> +fptr main() {
> +  return (fptr) 0;
> +}
> +
> +// expected-error at +2 {{conflicting types for 'main}}
> +// expected-warning at +1 {{return type of 'main' is not 'int'}}
> +void *(*main())(int a) {
> +  return (fptr) 0;
> +}
> +
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits



More information about the cfe-commits mailing list