[PATCH] D129061: [Lex] Diagnose macro in command lines

Jack Huang via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Jul 3 23:59:35 PDT 2022


jackhong12 created this revision.
jackhong12 added a reviewer: aaron.ballman.
Herald added a project: All.
jackhong12 requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

- Fixes https://github.com/llvm/llvm-project/issues/56159

Description
-----------

We need to diagnose the macro in commands too. For example, the expected result of `clang++ -Wreserved-identifier -D__WHY_NOT_ME__ -U__STDC__ tmp.cpp` should be

  <command line>:1:9: warning: macro name is a reserved identifier [-Wreserved-macro-identifier]
  #define __WHY_NOT_ME__ 1
          ^
  <command line>:2:8: warning: macro name is a reserved identifier [-Wreserved-macro-identifier]
  #undef __STDC__
         ^

Currently, clang will not show any warning message when we define or undefine reserved macro in commands.

Fix
---

We use digital directives in the preprocessor, like

  # 1 "<built-in>" 3
  #define __llvm__ 1
  #define __clang__ 1
  
  ...
  
  # 1 "<command line>" 1
  #define __WHY_NOT_ME__ 1
  #undef __STDC__
  #define __GCC_HAVE_DWARF2_CFI_ASM 1
  # 1 "<built-in>" 2

I think we should use `PresumedLoc` to determine its `<built-in>` or `<command line>` section. In this case, the file name will be changed from `<built-in>` to `<command line>`.  However,  `SourceMgr.getBufferName` only returns the first name it matches.

Another issue is that clang will define `__GCC_HAVE_DWARF2_CFI_ASM` in the command line. But I don't know how to handle this macro. I only add this macro to the reserved macro list.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D129061

Files:
  clang/lib/Lex/PPDirectives.cpp


Index: clang/lib/Lex/PPDirectives.cpp
===================================================================
--- clang/lib/Lex/PPDirectives.cpp
+++ clang/lib/Lex/PPDirectives.cpp
@@ -164,6 +164,7 @@
         "_THREAD_SAFE",
         "_XOPEN_SOURCE",
         "_XOPEN_SOURCE_EXTENDED",
+        "__GCC_HAVE_DWARF2_CFI_ASM",
         "__STDCPP_WANT_MATH_SPEC_FUNCS__",
         "__STDC_FORMAT_MACROS",
     };
@@ -352,8 +353,9 @@
   SourceLocation MacroNameLoc = MacroNameTok.getLocation();
   if (ShadowFlag)
     *ShadowFlag = false;
-  if (!SourceMgr.isInSystemHeader(MacroNameLoc) &&
-      (SourceMgr.getBufferName(MacroNameLoc) != "<built-in>")) {
+  PresumedLoc PL = SourceMgr.getPresumedLoc(MacroNameLoc);
+  if (!SourceMgr.isInSystemHeader(MacroNameLoc) && PL.isValid() &&
+      strcmp(PL.getFilename(), "<built-in>")) {
     MacroDiag D = MD_NoWarn;
     if (isDefineUndef == MU_Define) {
       D = shouldWarnOnMacroDef(*this, II);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D129061.442012.patch
Type: text/x-patch
Size: 939 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220704/c41891eb/attachment-0001.bin>


More information about the cfe-commits mailing list