]> granicus.if.org Git - clang/commitdiff
Improve 0-argument -Wvexing-parse diagnostic by adding notes with fix-its:
authorRichard Smith <richard-llvm@metafoo.co.uk>
Thu, 12 Jan 2012 23:53:29 +0000 (23:53 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Thu, 12 Jan 2012 23:53:29 +0000 (23:53 +0000)
 - If the declarator is at the start of a line, and the previous line contained
   another declarator and ended with a comma, then that comma was probably a
   typo for a semicolon:

   int n = 0, m = 1, l = 2, // k = 5;
   myImportantFunctionCall(); // oops!

 - If removing the parentheses would correctly initialize the object, then
   produce a note suggesting that fix.

 - Otherwise, if there is a simple initializer we can suggest which performs
   value-initialization, then provide a note suggesting a correction to that
   initializer.

Sema::Declarator now tracks the location of the comma prior to the declarator in
the declaration, if there is one, to facilitate providing the note. The code to
determine an appropriate initializer from the -Wuninitialized warning has been
factored out to allow use in both that and -Wvexing-parse.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@148072 91177308-0d34-0410-b5e6-96231b3b80d8

15 files changed:
include/clang/Basic/DiagnosticSemaKinds.td
include/clang/Sema/DeclSpec.h
include/clang/Sema/Sema.h
lib/Parse/ParseDecl.cpp
lib/Parse/ParseDeclCXX.cpp
lib/Parse/Parser.cpp
lib/Sema/AnalysisBasedWarnings.cpp
lib/Sema/SemaDecl.cpp
lib/Sema/SemaFixItUtils.cpp
test/CXX/basic/basic.link/p9.cpp
test/CXX/dcl.dcl/basic.namespace/namespace.udecl/p11.cpp
test/FixIt/fixit-vexing-parse.cpp [new file with mode: 0644]
test/SemaCXX/condition.cpp
test/SemaCXX/conditional-expr.cpp
test/SemaCXX/decl-expr-ambiguity.cpp

index 4341dbbfa916c0315900d57f309954e13705460f..fbd8098a40e9a8d13517355b4fc7edaee9fca438 100644 (file)
@@ -115,6 +115,12 @@ def warn_decl_in_param_list : Warning<
 def warn_empty_parens_are_function_decl : Warning<
   "empty parentheses interpreted as a function declaration">,
   InGroup<VexingParse>;
+def note_empty_parens_function_call : Note<
+  "change this ',' to a ';' to call %0">;
+def note_empty_parens_default_ctor : Note<
+  "remove parentheses to declare a variable">;
+def note_empty_parens_zero_initialize : Note<
+  "replace parentheses with an initializer to declare a variable">;
 def warn_unused_function : Warning<"unused function %0">,
   InGroup<UnusedFunction>, DefaultIgnore;
 def warn_unused_member_function : Warning<"unused member function %0">,
index a601210bf4b6617f62e34b11d3ce5381aff24977..edc6fd3bc90786adb42695902143fbda4654f035 100644 (file)
@@ -1468,6 +1468,10 @@ private:
   /// Extension - true if the declaration is preceded by __extension__.
   bool Extension : 1;
 
+  /// \brief If this is the second or subsequent declarator in this declaration,
+  /// the location of the comma before this declarator.
+  SourceLocation CommaLoc;
+
   /// \brief If provided, the source location of the ellipsis used to describe
   /// this declarator as a parameter pack.
   SourceLocation EllipsisLoc;
@@ -1557,6 +1561,8 @@ public:
     Attrs.clear();
     AsmLabel = 0;
     InlineParamsUsed = false;
+    CommaLoc = SourceLocation();
+    EllipsisLoc = SourceLocation();
   }
 
   /// mayOmitIdentifier - Return true if the identifier is either optional or
@@ -1846,7 +1852,11 @@ public:
 
   void setGroupingParens(bool flag) { GroupingParens = flag; }
   bool hasGroupingParens() const { return GroupingParens; }
-  
+
+  bool isFirstDeclarator() const { return !CommaLoc.isValid(); }
+  SourceLocation getCommaLoc() const { return CommaLoc; }
+  void setCommaLoc(SourceLocation CL) { CommaLoc = CL; }
+
   bool hasEllipsis() const { return EllipsisLoc.isValid(); }
   SourceLocation getEllipsisLoc() const { return EllipsisLoc; }
   void setEllipsisLoc(SourceLocation EL) { EllipsisLoc = EL; }
index aa0a7ef957a7e314165ddd746ed16d2b03d71ada..d570a779366648caa0994a2df6625f54df70aade 100644 (file)
@@ -756,6 +756,9 @@ public:
 
   bool findMacroSpelling(SourceLocation &loc, StringRef name);
 
+  /// \brief Get a string to suggest for zero-initialization of a type.
+  const char *getFixItZeroInitializerForType(QualType T) const;
+
   ExprResult Owned(Expr* E) { return E; }
   ExprResult Owned(ExprResult R) { return R; }
   StmtResult Owned(Stmt* S) { return S; }
index be5d5ae54cdbc2b224debbc9114b487cfee4a3d6..032326cea71669f8bba72de802cba0de92b96c85 100644 (file)
@@ -1138,6 +1138,7 @@ Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
 
     // Parse the next declarator.
     D.clear();
+    D.setCommaLoc(CommaLoc);
 
     // Accept attributes in an init-declarator.  In the first declarator in a
     // declaration, these would be part of the declspec.  In subsequent
@@ -2665,9 +2666,11 @@ ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
 
   // Read struct-declarators until we find the semicolon.
   bool FirstDeclarator = true;
+  SourceLocation CommaLoc;
   while (1) {
     ParsingDeclRAIIObject PD(*this);
     FieldDeclarator DeclaratorInfo(DS);
+    DeclaratorInfo.D.setCommaLoc(CommaLoc);
 
     // Attributes are only allowed here on successive declarators.
     if (!FirstDeclarator)
@@ -2703,7 +2706,7 @@ ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
       return;
 
     // Consume the comma.
-    ConsumeToken();
+    CommaLoc = ConsumeToken();
 
     FirstDeclarator = false;
   }
index 285dbbbe079a734be9352bad0b08439c1881cdf3..c31c55ba5b9a0e6dd1bd95d7adf02ca6644e0f57 100644 (file)
@@ -2044,6 +2044,7 @@ void Parser::ParseCXXClassMemberDeclaration(AccessSpecifier AS,
     BitfieldSize = true;
     Init = true;
     HasInitializer = false;
+    DeclaratorInfo.setCommaLoc(CommaLoc);
 
     // Attributes are only allowed on the second declarator.
     MaybeParseGNUAttributes(DeclaratorInfo);
index d14b9adce2cc1991015bd8227520f6b8a4489ac9..e09ee79c8cb927b7a433dfccf8f3e7ad0ac12af4 100644 (file)
@@ -1067,11 +1067,12 @@ void Parser::ParseKNRParamDeclarations(Declarator &D) {
       if (Tok.isNot(tok::comma))
         break;
 
+      ParmDeclarator.clear();
+
       // Consume the comma.
-      ConsumeToken();
+      ParmDeclarator.setCommaLoc(ConsumeToken());
 
       // Parse the next declarator.
-      ParmDeclarator.clear();
       ParseDeclarator(ParmDeclarator);
     }
 
index 3567b6bbe0680cd30a4c3817b6de3ef6faf4a23e..9b3191b02732e45d9acdbafa751214ccb7407d37 100644 (file)
@@ -415,42 +415,15 @@ static bool SuggestInitializationFixit(Sema &S, const VarDecl *VD) {
     return false;
 
   // Suggest possible initialization (if any).
-  const char *initialization = 0;
   QualType VariableTy = VD->getType().getCanonicalType();
-
-  if (VariableTy->isObjCObjectPointerType() ||
-      VariableTy->isBlockPointerType()) {
-    // Check if 'nil' is defined.
-    if (S.PP.getMacroInfo(&S.getASTContext().Idents.get("nil")))
-      initialization = " = nil";
-    else
-      initialization = " = 0";
-  }
-  else if (VariableTy->isRealFloatingType())
-    initialization = " = 0.0";
-  else if (VariableTy->isBooleanType() && S.Context.getLangOptions().CPlusPlus)
-    initialization = " = false";
-  else if (VariableTy->isEnumeralType())
+  const char *Init = S.getFixItZeroInitializerForType(VariableTy);
+  if (!Init)
     return false;
-  else if (VariableTy->isPointerType() || VariableTy->isMemberPointerType()) {
-    if (S.Context.getLangOptions().CPlusPlus0x)
-      initialization = " = nullptr";
-    // Check if 'NULL' is defined.
-    else if (S.PP.getMacroInfo(&S.getASTContext().Idents.get("NULL")))
-      initialization = " = NULL";
-    else
-      initialization = " = 0";
-  }
-  else if (VariableTy->isScalarType())
-    initialization = " = 0";
 
-  if (initialization) {
-    SourceLocation loc = S.PP.getLocForEndOfToken(VD->getLocEnd());
-    S.Diag(loc, diag::note_var_fixit_add_initialization) << VD->getDeclName()
-      << FixItHint::CreateInsertion(loc, initialization);
-    return true;
-  }
-  return false;
+  SourceLocation Loc = S.PP.getLocForEndOfToken(VD->getLocEnd());
+  S.Diag(Loc, diag::note_var_fixit_add_initialization) << VD->getDeclName()
+    << FixItHint::CreateInsertion(Loc, Init);
+  return true;
 }
 
 /// DiagnoseUninitializedUse -- Helper function for diagnosing uses of an
index dc8d6ec8cf75c21cba43f5a83028123c927c1541..4dcf1db2cf868784d8515efdcf60b453869ca49d 100644 (file)
@@ -4921,8 +4921,39 @@ Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC,
       if (!T->isVoidType() && C.Fun.NumArgs == 0 && !C.Fun.isVariadic &&
           !C.Fun.TrailingReturnType &&
           C.Fun.getExceptionSpecType() == EST_None) {
-        Diag(C.Loc, diag::warn_empty_parens_are_function_decl)
-          << SourceRange(C.Loc, C.EndLoc);
+        SourceRange ParenRange(C.Loc, C.EndLoc);
+        Diag(C.Loc, diag::warn_empty_parens_are_function_decl) << ParenRange;
+
+        // If the declaration looks like:
+        //   T var1,
+        //   f();
+        // and name lookup finds a function named 'f', then the ',' was
+        // probably intended to be a ';'.
+        if (!D.isFirstDeclarator() && D.getIdentifier()) {
+          FullSourceLoc Comma(D.getCommaLoc(), SourceMgr);
+          FullSourceLoc Name(D.getIdentifierLoc(), SourceMgr);
+          if (Comma.getFileID() != Name.getFileID() ||
+              Comma.getSpellingLineNumber() != Name.getSpellingLineNumber()) {
+            LookupResult Result(*this, D.getIdentifier(), SourceLocation(),
+                                LookupOrdinaryName);
+            if (LookupName(Result, S))
+              Diag(D.getCommaLoc(), diag::note_empty_parens_function_call)
+                << FixItHint::CreateReplacement(D.getCommaLoc(), ";") << NewFD;
+          }
+        }
+        const CXXRecordDecl *RD = T->getAsCXXRecordDecl();
+        // Empty parens mean value-initialization, and no parens mean default
+        // initialization. These are equivalent if the default constructor is
+        // user-provided, or if zero-initialization is a no-op.
+        if (RD && (RD->isEmpty() || RD->hasUserProvidedDefaultConstructor()))
+          Diag(C.Loc, diag::note_empty_parens_default_ctor)
+            << FixItHint::CreateRemoval(ParenRange);
+        else if (const char *Init = getFixItZeroInitializerForType(T))
+          Diag(C.Loc, diag::note_empty_parens_zero_initialize)
+            << FixItHint::CreateReplacement(ParenRange, Init);
+        else if (LangOpts.CPlusPlus0x)
+          Diag(C.Loc, diag::note_empty_parens_zero_initialize)
+            << FixItHint::CreateReplacement(ParenRange, "{}");
       }
     }
 
index 8e8a46da73e4d8378e2397b7a980f7ee76f3d778..1f17a9e83e73e109e19138841080b68095245a3f 100644 (file)
@@ -158,3 +158,31 @@ bool ConversionFixItGenerator::tryToFixConversion(const Expr *FullExpr,
 
   return false;
 }
+
+const char *Sema::getFixItZeroInitializerForType(QualType T) const {
+  // Suggest 'nil' if it's defined and appropriate.
+  if ((T->isObjCObjectPointerType() || T->isBlockPointerType()) &&
+      PP.getMacroInfo(&getASTContext().Idents.get("nil")))
+    return " = nil";
+  if (T->isRealFloatingType())
+    return " = 0.0";
+  if (T->isBooleanType() && LangOpts.CPlusPlus)
+    return " = false";
+  if (T->isPointerType() || T->isMemberPointerType()) {
+    if (LangOpts.CPlusPlus0x)
+      return " = nullptr";
+    // Check if 'NULL' is defined.
+    else if (PP.getMacroInfo(&getASTContext().Idents.get("NULL")))
+      return " = NULL";
+  }
+  if (T->isEnumeralType())
+    return 0;
+  if (T->isScalarType())
+    return " = 0";
+  const CXXRecordDecl *RD = T->getAsCXXRecordDecl();
+  if (LangOpts.CPlusPlus0x && RD && !RD->hasUserProvidedDefaultConstructor())
+    return "{}";
+  if (T->isAggregateType())
+    return " = {}";
+  return 0;
+}
index c895253c68fb6bb892d553efe3b0a141f36cc321..680c93db2e29a5beb4c8c608b553281a49ffc34c 100644 (file)
@@ -6,5 +6,5 @@ namespace N { } // expected-note{{here}}
 // First bullet: two names with external linkage that refer to
 // different kinds of entities.
 void f() {
-  int N(); // expected-error{{redefinition}} expected-warning{{interpreted as a function declaration}}
+  int N(); // expected-error{{redefinition}} expected-warning{{interpreted as a function declaration}} expected-note {{replace parentheses with an initializer}}
 }
index b1dcf4d0154368234008b508467250a694a45fdf..99903b57bf8cef1e47fc7afb1ffa5736ec820d39 100644 (file)
 
 namespace test0 {
   namespace ns { void foo(); } // expected-note {{target of using declaration}}
-  int foo(); // expected-note {{conflicting declaration}}
+  int foo(void); // expected-note {{conflicting declaration}}
   using ns::foo; // expected-error {{target of using declaration conflicts with declaration already in scope}}
 }
 
 namespace test1 {
   namespace ns { void foo(); } // expected-note {{target of using declaration}}
   using ns::foo; //expected-note {{using declaration}}
-  int foo(); // expected-error {{declaration conflicts with target of using declaration already in scope}}
+  int foo(void); // expected-error {{declaration conflicts with target of using declaration already in scope}}
 }
 
 namespace test2 {
   namespace ns { void foo(); } // expected-note 2 {{target of using declaration}}
   void test0() {
-    int foo(); // expected-note {{conflicting declaration}} expected-warning{{function declaration}}
+    int foo(void); // expected-note {{conflicting declaration}}
     using ns::foo; // expected-error {{target of using declaration conflicts with declaration already in scope}}
   }
 
   void test1() {
     using ns::foo; //expected-note {{using declaration}}
-    int foo(); // expected-error {{declaration conflicts with target of using declaration already in scope}} expected-warning{{function declaration}}
+    int foo(void); // expected-error {{declaration conflicts with target of using declaration already in scope}}
   }
 }
 
@@ -39,7 +39,7 @@ namespace test3 {
   namespace ns { void foo(); } // expected-note 2 {{target of using declaration}}
   class Test0 {
     void test() {
-      int foo(); // expected-note {{conflicting declaration}} expected-warning{{function declaration}}
+      int foo(void); // expected-note {{conflicting declaration}}
       using ns::foo; // expected-error {{target of using declaration conflicts with declaration already in scope}}
     }
   };
@@ -47,7 +47,7 @@ namespace test3 {
   class Test1 {
     void test() {
       using ns::foo; //expected-note {{using declaration}}
-      int foo(); // expected-error {{declaration conflicts with target of using declaration already in scope}} expected-warning{{function declaration}}
+      int foo(void); // expected-error {{declaration conflicts with target of using declaration already in scope}}
     }
   };
 }
@@ -56,7 +56,7 @@ namespace test4 {
   namespace ns { void foo(); } // expected-note 2 {{target of using declaration}}
   template <typename> class Test0 {
     void test() {
-      int foo(); // expected-note {{conflicting declaration}} expected-warning{{function declaration}}
+      int foo(void); // expected-note {{conflicting declaration}}
       using ns::foo; // expected-error {{target of using declaration conflicts with declaration already in scope}}
     }
   };
@@ -64,7 +64,7 @@ namespace test4 {
   template <typename> class Test1 {
     void test() {
       using ns::foo; //expected-note {{using declaration}}
-      int foo(); // expected-error {{declaration conflicts with target of using declaration already in scope}} expected-warning{{function declaration}}
+      int foo(void); // expected-error {{declaration conflicts with target of using declaration already in scope}}
     }
   };
 }
diff --git a/test/FixIt/fixit-vexing-parse.cpp b/test/FixIt/fixit-vexing-parse.cpp
new file mode 100644 (file)
index 0000000..0f0505f
--- /dev/null
@@ -0,0 +1,80 @@
+// RUN: %clang_cc1 -verify -x c++ %s
+// RUN: %clang_cc1 -fdiagnostics-parseable-fixits -x c++ %s 2>&1 | FileCheck %s
+
+struct S {
+  int n;
+};
+
+struct T {
+  T();
+  int n;
+};
+
+struct U {
+  ~U();
+  int n;
+};
+
+struct V {
+  ~V();
+};
+
+struct W : V {
+};
+
+struct X : U {
+};
+
+int F1();
+S F2();
+
+namespace N {
+  void test() {
+    // CHECK: fix-it:"{{.*}}":{34:9-34:11}:" = {}"
+    S s1(); // expected-warning {{function declaration}} expected-note {{replace parentheses with an initializer}}
+
+    // CHECK: fix-it:"{{.*}}":{38:9-38:10}:";"
+    // CHECK: fix-it:"{{.*}}":{39:7-39:9}:" = {}"
+    S s2, // expected-note {{change this ',' to a ';' to call 'F2'}}
+    F2(); // expected-warning {{function declaration}} expected-note {{replace parentheses with an initializer}}
+
+    // CHECK: fix-it:"{{.*}}":{43:9-43:11}:""
+    // CHECK: fix-it:"{{.*}}":{44:9-44:11}:""
+    T t1(), // expected-warning {{function declaration}} expected-note {{remove parentheses}}
+      t2(); // expected-warning {{function declaration}} expected-note {{remove parentheses}}
+
+    // CHECK: fix-it:"{{.*}}":{47:8-47:10}:" = {}"
+    U u(); // expected-warning {{function declaration}} expected-note {{replace parentheses with an initializer}}
+
+    // CHECK: fix-it:"{{.*}}":{50:8-50:10}:""
+    V v(); // expected-warning {{function declaration}} expected-note {{remove parentheses}}
+
+    // CHECK: fix-it:"{{.*}}":{53:8-53:10}:""
+    W w(); // expected-warning {{function declaration}} expected-note {{remove parentheses}}
+
+    // TODO: Removing the parens here would not initialize U::n.
+    // Maybe suggest an " = X()" initializer for this case?
+    // Maybe suggest removing the parens anyway?
+    X x(); // expected-warning {{function declaration}}
+
+    // CHECK: fix-it:"{{.*}}":{61:11-61:13}:" = 0"
+    int n1(); // expected-warning {{function declaration}} expected-note {{replace parentheses with an initializer}}
+
+    // CHECK: fix-it:"{{.*}}":{65:11-65:12}:";"
+    // CHECK: fix-it:"{{.*}}":{66:7-66:9}:" = 0"
+    int n2, // expected-note {{change this ',' to a ';' to call 'F1'}}
+    F1(); // expected-warning {{function declaration}} expected-note {{replace parentheses with an initializer}}
+
+    // CHECK: fix-it:"{{.*}}":{69:13-69:15}:" = 0.0"
+    double d(); // expected-warning {{function declaration}} expected-note {{replace parentheses with an initializer}}
+
+    typedef void *Ptr;
+
+    // CHECK: fix-it:"{{.*}}":{74:10-74:12}:" = 0"
+    Ptr p(); // expected-warning {{function declaration}} expected-note {{replace parentheses with an initializer}}
+
+#define NULL 0
+    // CHECK: fix-it:"{{.*}}":{78:10-78:12}:" = NULL"
+    Ptr p(); // expected-warning {{function declaration}} expected-note {{replace parentheses with an initializer}}
+  }
+}
index 3d17dc39377e84ed1971098d158dba3a84937094..43ee640e0f6cb8fb9ee0b87d33df002087cade36 100644 (file)
@@ -7,7 +7,7 @@ void test() {
 
   typedef int arr[10];
   while (arr x=0) ; // expected-error {{an array type is not allowed here}} expected-error {{array initializer must be an initializer list}}
-  while (int f()=0) ; // expected-warning {{interpreted as a function declaration}} expected-error {{a function type is not allowed here}}
+  while (int f()=0) ; // expected-warning {{interpreted as a function declaration}} expected-note {{initializer}} expected-error {{a function type is not allowed here}}
 
   struct S {} s;
   if (s) ++x; // expected-error {{value of type 'struct S' is not contextually convertible to 'bool'}}
index 3cfddb3a416b4f5ed98e28e2f5aa7845f56bd6ea..4aee913277e5296ec2830be34734c91b1b9bc5d1 100644 (file)
@@ -96,8 +96,8 @@ void test()
   (void)(i1 ? BadDerived() : BadBase());
 
   // b2.1 (hierarchy stuff)
-  const Base constret(); // expected-warning {{interpreted as a function declaration}}
-  const Derived constder(); // expected-warning {{interpreted as a function declaration}}
+  extern const Base constret();
+  extern const Derived constder();
   // should use const overload
   A a1((i1 ? constret() : Base()).trick());
   A a2((i1 ? Base() : constret()).trick());
index 3a946c0ffb949ff81e44bd64022f1afc0ca519cd..f6dbe2f3398e4bdfe52862128f9db312f2793a8a 100644 (file)
@@ -26,15 +26,15 @@ void f() {
   T(*d)(int(p)); // expected-warning {{parentheses were disambiguated as a function declarator}} expected-note {{previous definition is here}}
   typedef T(*td)(int(p));
   extern T(*tp)(int(p));
-  T d3(); // expected-warning {{empty parentheses interpreted as a function declaration}}
+  T d3(); // expected-warning {{empty parentheses interpreted as a function declaration}} expected-note {{replace parentheses with an initializer}}
   T d3v(void);
   typedef T d3t();
   extern T f3();
-  __typeof(*T()) f4(); // expected-warning {{empty parentheses interpreted as a function declaration}}
+  __typeof(*T()) f4(); // expected-warning {{empty parentheses interpreted as a function declaration}} expected-note {{replace parentheses with an initializer}}
   typedef void *V;
   __typeof(*V()) f5();
   T multi1,
-    multi2(); // expected-warning {{empty parentheses interpreted as a function declaration}}
+    multi2(); // expected-warning {{empty parentheses interpreted as a function declaration}} expected-note {{replace parentheses with an initializer}}
   T(d)[5]; // expected-error {{redefinition of 'd'}}
   typeof(int[])(f) = { 1, 2 }; // expected-error {{extension used}}
   void(b)(int);
@@ -43,6 +43,20 @@ void f() {
   int(d3(int()));
 }
 
+struct RAII {
+  RAII();
+  ~RAII();
+};
+
+void func();
+namespace N {
+  void emptyParens() {
+    RAII raii(); // expected-warning {{function declaration}} expected-note {{remove parentheses to declare a variable}}
+    int a, b, c, d, e, // expected-note {{change this ',' to a ';' to call 'func'}}
+    func(); // expected-warning {{function declaration}} expected-note {{replace parentheses with an initializer}}
+  }
+}
+
 class C { };
 void fn(int(C)) { } // void fn(int(*fp)(C c)) { } expected-note{{candidate function}}
                     // not: void fn(int C);