[clang-tools-extra] `cppcoreguidelines-macro-usage`: Skip common macros which cannot be converted to `constexpr` (PR #80797)

via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 6 11:44:37 PST 2024


================
@@ -80,6 +80,20 @@ void MacroUsageCheck::warnMacro(const MacroDirective *MD, StringRef MacroName) {
   const MacroInfo *Info = MD->getMacroInfo();
   StringRef Message;
 
+  for (const auto &T : MD->getMacroInfo()->tokens()) {
+    if (T.is(tok::hash)) {
+      return;
+    }
+    if (T.is(tok::identifier)) {
+      StringRef IdentName = T.getIdentifierInfo()->getName();
+      if (IdentName == "__FILE__") {
+        return;
+      } else if (IdentName == "__LINE__") {
+        return;
+      }
+    }
+  }
+
----------------
bwrsandman wrote:

If I understand `MacroExpands` macro, it only gets invoked if the macro is used therefore it would need to be called somewhere for the check to trigger. Your code snippet will still error out with `MacroDefined` and `MacroExpands` won't ever be invoked.

Is there a way to use the post-processor inside of `MacroDefined` to expand the definition?

https://github.com/llvm/llvm-project/pull/80797


More information about the cfe-commits mailing list