[PATCH] D81003: [clang][Sema] SequenceChecker: Also visit default arguments.

Bruno Ricci via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 2 07:08:53 PDT 2020


riccibruno created this revision.
riccibruno added a reviewer: rsmith.
riccibruno added a project: clang.
Herald added a subscriber: cfe-commits.

`SequenceChecker` is currently not visiting default arguments and therefore missing cases like:

  int a;
  int foo(int x = a++, int y = a);
  void test() {
    foo(); // should warn
    foo(a++); // idem
  }


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D81003

Files:
  clang/lib/Sema/SemaChecking.cpp
  clang/test/SemaCXX/warn-unsequenced.cpp


Index: clang/test/SemaCXX/warn-unsequenced.cpp
===================================================================
--- clang/test/SemaCXX/warn-unsequenced.cpp
+++ clang/test/SemaCXX/warn-unsequenced.cpp
@@ -268,6 +268,36 @@
                                           // cxx17-warning at -1 {{unsequenced modification and access to 'pf'}}
 }
 
+namespace default_arg {
+  int a;
+  void f1(int = a, int = a++); // cxx11-warning 2{{unsequenced modification and access to 'a'}}
+                               // cxx17-warning at -1 2{{unsequenced modification and access to 'a'}}
+
+  void f2(int = a++, int = a); // cxx11-warning {{unsequenced modification and access to 'a'}}
+                               // cxx17-warning at -1 {{unsequenced modification and access to 'a'}}
+
+  void f3(int = a++, int = sizeof(a));
+
+  void test() {
+    // TODO: Consider adding a remark indicating the function call where
+    // the default argument was used.
+    int b;
+    f1();
+    f1(a);
+    f1(a,a);     // ok
+    f1(b);       // ok
+    f1(b,a++);   // ok
+
+    f2();
+    f2(a);       // ok
+    f2(a++);     // cxx11-warning {{unsequenced modification and access to 'a'}}
+                 // cxx17-warning at -1 {{unsequenced modification and access to 'a'}}
+
+    f3();       // ok
+    f3(a++);    // ok
+  }
+}
+
 namespace PR20819 {
   struct foo { void bar(int); };
   foo get_foo(int);
Index: clang/lib/Sema/SemaChecking.cpp
===================================================================
--- clang/lib/Sema/SemaChecking.cpp
+++ clang/lib/Sema/SemaChecking.cpp
@@ -12977,6 +12977,10 @@
     });
   }
 
+  void VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *DAE) {
+    Visit(DAE->getExpr());
+  }
+
   void VisitCXXConstructExpr(const CXXConstructExpr *CCE) {
     // This is a call, so all subexpressions are sequenced before the result.
     SequencedSubexpression Sequenced(*this);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D81003.267868.patch
Type: text/x-patch
Size: 1892 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200602/1bddf7b7/attachment-0001.bin>


More information about the cfe-commits mailing list