]> granicus.if.org Git - clang/commitdiff
Loosen -Wempty-body warning
authorReid Kleckner <rnk@google.com>
Fri, 17 Nov 2017 21:33:28 +0000 (21:33 +0000)
committerReid Kleckner <rnk@google.com>
Fri, 17 Nov 2017 21:33:28 +0000 (21:33 +0000)
Do not show it when `if` or `else` come from macros.
E.g.,

    #define USED(A) if (A); else
    #define SOME_IF(A) if (A)

    void test() {
      // No warnings are shown in those cases now.
      USED(0);
      SOME_IF(0);
    }

Patch by Ilya Biryukov!

Differential Revision: https://reviews.llvm.org/D40185

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

include/clang/Sema/Sema.h
lib/Parse/ParseStmt.cpp
lib/Sema/SemaChecking.cpp
lib/Sema/SemaStmt.cpp
test/SemaCXX/warn-empty-body.cpp

index 66328adad6081c8eb520804a24f682db8073cbb1..8133995c8dfce2885accc99065a1370286db9110 100644 (file)
@@ -9690,6 +9690,7 @@ public:
   class ConditionResult {
     Decl *ConditionVar;
     FullExprArg Condition;
+    SourceLocation RParenLoc;
     bool Invalid;
     bool HasKnownValue;
     bool KnownValue;
@@ -9713,6 +9714,9 @@ public:
       return std::make_pair(cast_or_null<VarDecl>(ConditionVar),
                             Condition.get());
     }
+
+    void setRParenLoc(SourceLocation Loc) { RParenLoc = Loc; }
+
     llvm::Optional<bool> getKnownValue() const {
       if (!HasKnownValue)
         return None;
index e8cf7d5fa4578610cd56858b11bceaf549c8ea87..ec14cee5a3485f997824d73dc9c24cb8299f16d9 100644 (file)
@@ -1101,6 +1101,7 @@ bool Parser::ParseParenExprOrCondition(StmtResult *InitStmt,
 
   // Otherwise the condition is valid or the rparen is present.
   T.consumeClose();
+  Cond.setRParenLoc(T.getCloseLocation());
 
   // Check for extraneous ')'s to catch things like "if (foo())) {".  We know
   // that all callers are looking for a statement after the condition, so ")"
index e662a5c8b977c48531c4056f52acfa8f9110c31f..8a85830bfdb06337e93f5dc7e4a21cd57dc40500 100644 (file)
@@ -11821,7 +11821,7 @@ static bool ShouldDiagnoseEmptyStmtBody(const SourceManager &SourceMgr,
 
   // Get line numbers of statement and body.
   bool StmtLineInvalid;
-  unsigned StmtLine = SourceMgr.getPresumedLineNumber(StmtLoc,
+  unsigned StmtLine = SourceMgr.getSpellingLineNumber(StmtLoc,
                                                       &StmtLineInvalid);
   if (StmtLineInvalid)
     return false;
index 07b70305c196cefb5bc2db00c1477eb654b0fa57..de9b8c3b76cecf73c15a58e86d9a4c7fc13d5906 100644 (file)
@@ -530,8 +530,7 @@ Sema::ActOnIfStmt(SourceLocation IfLoc, bool IsConstexpr, Stmt *InitStmt,
   if (elseStmt)
     DiagnoseEmptyStmtBody(ElseLoc, elseStmt, diag::warn_empty_else_body);
   else
-    DiagnoseEmptyStmtBody(CondExpr->getLocEnd(), thenStmt,
-                          diag::warn_empty_if_body);
+    DiagnoseEmptyStmtBody(Cond.RParenLoc, thenStmt, diag::warn_empty_if_body);
 
   return BuildIfStmt(IfLoc, IsConstexpr, InitStmt, Cond, thenStmt, ElseLoc,
                      elseStmt);
index bd6b47f053f1a3faa5beda718d0959bab6151005..0fc0cd51f93b234e52f16ff181e33633f2b49b74 100644 (file)
@@ -301,3 +301,14 @@ void test7(int x, int y) {
   if (x) IDENTITY(); // no-warning
 }
 
+#define SOME_IF(A) if (A)
+#define IF_ELSE(A) if (A); else
+
+
+void test_macros() {
+  SOME_IF(0);
+  IF_ELSE(0);
+
+  IDENTITY(if (0);) // expected-warning{{if statement has empty body}} expected-note{{separate line}}
+  IDENTITY(if (0); else;) // expected-warning{{else clause has empty body}} expected-note{{separate line}}}
+}