[PATCH] clang-tidy: check for repeated side effects in macro

Alexander Kornienko alexfh at google.com
Mon Jun 8 06:53:41 PDT 2015


================
Comment at: clang-tidy/misc/MacroRepeatedSideEffectsCheck.cpp:74
@@ +73,3 @@
+      // macro to complex.
+      if (T.is(tok::question) || T.is(tok::kw_if) || T.is(tok::kw_else) ||
+          T.is(tok::kw_switch) || T.is(tok::kw_case) || T.is(tok::kw_break) ||
----------------
This would also benefit from `Token::isOneOf` that you can move from clang/lib/Format/FormatToken.h to clang::Token.

================
Comment at: clang-tidy/misc/MacroRepeatedSideEffectsCheck.cpp:96
@@ +95,3 @@
+      // skip next parenthesis.
+      if (TII->hasMacroDefinition() || TII->getBuiltinID() != 0) {
+        SkipParen = true;
----------------
You seem to assume that the macro is function-like, but you'd better check it (`PP->getMacroDefinition(TII)->getMacroInfo()->isFunctionLike()` + all required nullptr checks).

================
Comment at: clang-tidy/misc/MacroRepeatedSideEffectsCheck.cpp:123
@@ +122,3 @@
+      if (AT.is(tok::plusplus) || AT.is(tok::minusminus)) {
+        Check.diag(ResultArgToks->getLocation(), "side effects in the %0macro "
+                                                 "argument '%1' is repeated in "
----------------
Let's use standard facilities provided by the diagnostics engine:

  Check.diag(..., "side effects in the %ordinal0 macro argument ....

If you want this to be more verbose, you can combine this with a `%select{}` modifier to add spellings for the first few ordinals.

================
Comment at: test/clang-tidy/misc-repeated-side-effects-in-macro.c:66
@@ +65,3 @@
+	ret += builtinB(a++);
+	// CHECK-MESSAGES: :[[@LINE-1]]:18: warning: side effects in the first macro argument 'x' is repeated in macro expansion [misc-macro-repeated-side-effects]
+	ret += macroA(a++);
----------------
You can omit the " is repeated in macro expansion [misc-macro-repeated-side-effects]" part from all check lines but the first one to make the tests more readable.

http://reviews.llvm.org/D9496

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/






More information about the cfe-commits mailing list