This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH 7/7][committed] Remove semicolon after do {} while (0) in DEF_SANITIZER_BUILTIN


Hi,

Consider this code in gcc/asan.c:
...
#define DEF_SANITIZER_BUILTIN(ENUM, NAME, TYPE, ATTRS) \
          do { \
            decl = add_builtin_function ("__builtin_" NAME, TYPE, ENUM, \
                                         BUILT_IN_NORMAL, NAME, \
                                         NULL_TREE); \
            set_call_expr_flags (decl, ATTRS); \
            set_builtin_decl (ENUM, decl, true); \
          } while (0);

#include "sanitizer.def"

          if ((flag_sanitize & SANITIZE_OBJECT_SIZE)
              && !builtin_decl_implicit_p (BUILT_IN_OBJECT_SIZE))
            DEF_SANITIZER_BUILTIN (BUILT_IN_OBJECT_SIZE, "object_size",
                                   BT_FN_SIZE_CONST_PTR_INT,
                                   ATTR_PURE_NOTHROW_LEAF_LIST)
...

As explained here ( https://gcc.gnu.org/onlinedocs/cpp/Swallowing-the-Semicolon.html ), we can wrap a multi-statement macro body in a "do {} while (0)" in order to be able to use the macro call as if it were a single statement. However, adding a semicolon after that "do {} while (0)", as is done in DEF_SANITIZER_BUILTIN means you can no longer use it as a single statement (compiling "if DEF_SANITIZER_BUILTIN (...); else ...;" will give an "else without a previous if" compiler error). This problem is then handled in the if stmt by leaving out the semicolon after DEF_SANITIZER_BUILTIN, which is awkward.

OTOH, we use .def files with lists of macro calls to generate tables and code. When generating code, it's natural to use a semicolon as the last part of the macro body, in order to separate statements.

This patch factors out DEF_SANITIZER_BUILTIN_1 from DEF_SANITIZER_BUILTIN, and uses DEF_SANITIZER_BUILTIN_1 in the if statement as a normal single statement, as made possible by the "do {} while (0)".

The original DEF_SANITIZER_BUILTIN is still used with the .def file, as before.

Committed as obvious.

Thanks,
- Tom

2017-11-02  Tom de Vries  <tom@codesourcery.com>

	PR other/82784
	* asan.c (DEF_SANITIZER_BUILTIN_1): Factor out of ...
	(DEF_SANITIZER_BUILTIN): ... here.
	(initialize_sanitizer_builtins): Use DEF_SANITIZER_BUILTIN_1
	instead of DEF_SANITIZER_BUILTIN in if stmt.  Add missing
	semicolon.

Signed-off-by: Tom de Vries <tom@codesourcery.com>
---
         gcc/asan.c | 16 ++++++++++------
         1 file changed, 10 insertions(+), 6 deletions(-)






diff --git a/gcc/asan.c b/gcc/asan.c
index d5128aa..61e1e7a 100644
--- a/gcc/asan.c
+++ b/gcc/asan.c
@@ -2804,14 +2804,17 @@ initialize_sanitizer_builtins (void)
 #define ATTR_PURE_NOTHROW_LEAF_LIST ECF_PURE | ATTR_NOTHROW_LEAF_LIST
 #undef DEF_BUILTIN_STUB
 #define DEF_BUILTIN_STUB(ENUM, NAME)
-#undef DEF_SANITIZER_BUILTIN
-#define DEF_SANITIZER_BUILTIN(ENUM, NAME, TYPE, ATTRS) \
+#undef DEF_SANITIZER_BUILTIN_1
+#define DEF_SANITIZER_BUILTIN_1(ENUM, NAME, TYPE, ATTRS)		\
   do {									\
     decl = add_builtin_function ("__builtin_" NAME, TYPE, ENUM,		\
 				 BUILT_IN_NORMAL, NAME, NULL_TREE);	\
     set_call_expr_flags (decl, ATTRS);					\
     set_builtin_decl (ENUM, decl, true);				\
-  } while (0);
+  } while (0)
+#undef DEF_SANITIZER_BUILTIN
+#define DEF_SANITIZER_BUILTIN(ENUM, NAME, TYPE, ATTRS)	\
+  DEF_SANITIZER_BUILTIN_1 (ENUM, NAME, TYPE, ATTRS);
 
 #include "sanitizer.def"
 
@@ -2820,10 +2823,11 @@ initialize_sanitizer_builtins (void)
      DEF_SANITIZER_BUILTIN here only as a convenience macro.  */
   if ((flag_sanitize & SANITIZE_OBJECT_SIZE)
       && !builtin_decl_implicit_p (BUILT_IN_OBJECT_SIZE))
-    DEF_SANITIZER_BUILTIN (BUILT_IN_OBJECT_SIZE, "object_size",
-			   BT_FN_SIZE_CONST_PTR_INT,
-			   ATTR_PURE_NOTHROW_LEAF_LIST)
+    DEF_SANITIZER_BUILTIN_1 (BUILT_IN_OBJECT_SIZE, "object_size",
+			     BT_FN_SIZE_CONST_PTR_INT,
+			     ATTR_PURE_NOTHROW_LEAF_LIST);
 
+#undef DEF_SANITIZER_BUILTIN_1
 #undef DEF_SANITIZER_BUILTIN
 #undef DEF_BUILTIN_STUB
 }










Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]