[clang] [clang][frontend] Make DumpModuleInfoAction emit the full macro (PR #85745)

2024-03-19 Thread Zhang Yi via cfe-commits

https://github.com/zhanyi22333 created 
https://github.com/llvm/llvm-project/pull/85745

This PR works on the TODO `Emit the macro definition bodies completely`  in 
FrontendActions.cpp. Emiting the macro definition bodies completely can help 
user get more details about macro.

The beginning of macro-body string is get by MacroInfo definition location in 
SourceManager. Then it compute the macro-body string length by locate the '\n' 
but not '\\\n'. After that, the string is getted and reformmatted into one 
line. 
When cannot get the MacroInfo, it will only emit the name of macro definition. 

@rjmccall @kpneal @zahiraam @efriedma-quic @vgvassilev @junaire

>From d29678159c34a668259fbed601df975275190105 Mon Sep 17 00:00:00 2001
From: Zhang Yi <18994118...@163.com>
Date: Mon, 18 Mar 2024 20:22:39 -0700
Subject: [PATCH 1/2] [clang][frontend] change DumpModuleInfoAction test cases.

---
 clang/test/Modules/cxx20-module-file-info-macros.cpp | 8 
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/clang/test/Modules/cxx20-module-file-info-macros.cpp 
b/clang/test/Modules/cxx20-module-file-info-macros.cpp
index 3b67e9b9acd410..fad218bdfc9638 100644
--- a/clang/test/Modules/cxx20-module-file-info-macros.cpp
+++ b/clang/test/Modules/cxx20-module-file-info-macros.cpp
@@ -37,8 +37,8 @@
 
 // CHECK: Macro Definitions:
 // CHECK-DAG: REDEFINE
-// CHECK-DAG: FUNC_Macro
-// CHECK-DAG: CONSTANT
+// CHECK-DAG: FUNC_Macro(X) (X+1)
+// CHECK-DAG: CONSTANT 43
 // CHECK-DAG: FOO
 // CHECK-NEXT: ===
 
@@ -46,8 +46,8 @@
 #include "foo.h"
 #undef REDEFINE
 // CHECK: Macro Definitions:
-// CHECK-DAG: CONSTANT
-// CHECK-DAG: FUNC_Macro
+// CHECK-DAG: CONSTANT 43
+// CHECK-DAG: FUNC_Macro(X) (X+1)
 // CHECK-DAG: FOO
 // CHECK-NEXT: ===
 

>From ce6501638142a9cbce7733ff9acab70f82d65891 Mon Sep 17 00:00:00 2001
From: Zhang Yi <18994118...@163.com>
Date: Mon, 18 Mar 2024 20:23:00 -0700
Subject: [PATCH 2/2] [clang][frontend] Make DumpModuleInfoAction emit the full
 macro

---
 clang/lib/Frontend/FrontendActions.cpp | 32 +++---
 1 file changed, 29 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Frontend/FrontendActions.cpp 
b/clang/lib/Frontend/FrontendActions.cpp
index 81fcd8d5ae9bd3..aa2781daef3988 100644
--- a/clang/lib/Frontend/FrontendActions.cpp
+++ b/clang/lib/Frontend/FrontendActions.cpp
@@ -925,15 +925,41 @@ void DumpModuleInfoAction::ExecuteAction() {
 
 // Emit the macro definitions in the module file so that we can know how
 // much definitions in the module file quickly.
-// TODO: Emit the macro definition bodies completely.
 if (auto FilteredMacros = llvm::make_filter_range(
 R->getPreprocessor().macros(),
 [](const auto &Macro) { return Macro.first->isFromAST(); });
 !FilteredMacros.empty()) {
   Out << "   Macro Definitions:\n";
   for (/* pair*/ const auto &Macro :
-   FilteredMacros)
-Out << " " << Macro.first->getName() << "\n";
+   FilteredMacros) {
+SourceManager &SM = PP.getSourceManager();
+clang::MacroInfo *MacroInfo = PP.getMacroInfo(Macro.first);
+if (MacroInfo) {
+  bool isInvalid=true;
+  const char* 
ContentStrPtr=SM.getCharacterData(MacroInfo->getDefinitionLoc(),&isInvalid);
+  if (!isInvalid) {
+  int ContentLength=0;
+  while ( !(*(ContentStrPtr+ContentLength)=='\n') ||  
*(ContentStrPtr+ContentLength-1)=='\\') {
+ContentLength++;
+  }
+  std::string MacroContent=std::string(ContentStrPtr,ContentLength);
+  // Replace '\\\n' with space
+  size_t pos = 0;
+  while ((pos = MacroContent.find("\\\n", pos)) != std::string::npos) {
+MacroContent.replace(pos, 2," ");
+  }
+  // Merge spaces
+  auto new_end = std::unique(MacroContent.begin(), MacroContent.end(), 
[](char a, char b) {
+return a == ' ' && b == ' ';
+  });
+  MacroContent.erase(new_end, MacroContent.end());
+  Out << " " << MacroContent << '\n';
+  }
+}
+else {
+  Out << " " << Macro.first->getName() << "\n";
+}
+  }
 }
 
 // Now let's print out any modules we did not see as part of the Primary.

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][frontend] Make DumpModuleInfoAction emit the full macro (PR #85745)

2024-03-19 Thread Zhang Yi via cfe-commits

https://github.com/zhanyi22333 updated 
https://github.com/llvm/llvm-project/pull/85745

>From d29678159c34a668259fbed601df975275190105 Mon Sep 17 00:00:00 2001
From: Zhang Yi <18994118...@163.com>
Date: Mon, 18 Mar 2024 20:22:39 -0700
Subject: [PATCH 1/2] [clang][frontend] change DumpModuleInfoAction test cases.

---
 clang/test/Modules/cxx20-module-file-info-macros.cpp | 8 
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/clang/test/Modules/cxx20-module-file-info-macros.cpp 
b/clang/test/Modules/cxx20-module-file-info-macros.cpp
index 3b67e9b9acd410..fad218bdfc9638 100644
--- a/clang/test/Modules/cxx20-module-file-info-macros.cpp
+++ b/clang/test/Modules/cxx20-module-file-info-macros.cpp
@@ -37,8 +37,8 @@
 
 // CHECK: Macro Definitions:
 // CHECK-DAG: REDEFINE
-// CHECK-DAG: FUNC_Macro
-// CHECK-DAG: CONSTANT
+// CHECK-DAG: FUNC_Macro(X) (X+1)
+// CHECK-DAG: CONSTANT 43
 // CHECK-DAG: FOO
 // CHECK-NEXT: ===
 
@@ -46,8 +46,8 @@
 #include "foo.h"
 #undef REDEFINE
 // CHECK: Macro Definitions:
-// CHECK-DAG: CONSTANT
-// CHECK-DAG: FUNC_Macro
+// CHECK-DAG: CONSTANT 43
+// CHECK-DAG: FUNC_Macro(X) (X+1)
 // CHECK-DAG: FOO
 // CHECK-NEXT: ===
 

>From 37e6d22a1aa4930521b85d13b7860a168416e256 Mon Sep 17 00:00:00 2001
From: Zhang Yi <18994118...@163.com>
Date: Mon, 18 Mar 2024 20:23:00 -0700
Subject: [PATCH 2/2] [clang][frontend] Make DumpModuleInfoAction emit the full
 macro

---
 clang/lib/Frontend/FrontendActions.cpp | 35 +++---
 1 file changed, 32 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Frontend/FrontendActions.cpp 
b/clang/lib/Frontend/FrontendActions.cpp
index 81fcd8d5ae9bd3..c6ed8231af0ecd 100644
--- a/clang/lib/Frontend/FrontendActions.cpp
+++ b/clang/lib/Frontend/FrontendActions.cpp
@@ -925,15 +925,44 @@ void DumpModuleInfoAction::ExecuteAction() {
 
 // Emit the macro definitions in the module file so that we can know how
 // much definitions in the module file quickly.
-// TODO: Emit the macro definition bodies completely.
 if (auto FilteredMacros = llvm::make_filter_range(
 R->getPreprocessor().macros(),
 [](const auto &Macro) { return Macro.first->isFromAST(); });
 !FilteredMacros.empty()) {
   Out << "   Macro Definitions:\n";
   for (/* pair*/ const auto &Macro :
-   FilteredMacros)
-Out << " " << Macro.first->getName() << "\n";
+   FilteredMacros) {
+SourceManager &SM = PP.getSourceManager();
+clang::MacroInfo *MacroInfo = PP.getMacroInfo(Macro.first);
+if (MacroInfo) {
+  bool isInvalid = true;
+  const char *ContentStrPtr =
+  SM.getCharacterData(MacroInfo->getDefinitionLoc(), &isInvalid);
+  if (!isInvalid) {
+int ContentLength = 0;
+while (!(*(ContentStrPtr + ContentLength) == '\n') ||
+   *(ContentStrPtr + ContentLength - 1) == '\\') {
+  ContentLength++;
+}
+std::string MacroContent =
+std::string(ContentStrPtr, ContentLength);
+// Replace '\\\n' with space
+size_t pos = 0;
+while ((pos = MacroContent.find("\\\n", pos)) !=
+   std::string::npos) {
+  MacroContent.replace(pos, 2, " ");
+}
+// Merge spaces
+auto new_end = std::unique(
+MacroContent.begin(), MacroContent.end(),
+[](char a, char b) { return a == ' ' && b == ' '; });
+MacroContent.erase(new_end, MacroContent.end());
+Out << " " << MacroContent << '\n';
+  }
+} else {
+  Out << " " << Macro.first->getName() << "\n";
+}
+  }
 }
 
 // Now let's print out any modules we did not see as part of the Primary.

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][frontend] Make DumpModuleInfoAction emit the full macro (PR #85745)

2024-04-21 Thread Zhang Yi via cfe-commits

https://github.com/zhanyi22333 updated 
https://github.com/llvm/llvm-project/pull/85745

>From 61b24269867dcec12c8fcab0c26b444f33a57454 Mon Sep 17 00:00:00 2001
From: root 
Date: Sun, 21 Apr 2024 16:43:17 +0800
Subject: [PATCH 1/2] [clang][frontend] change DumpModuleInfoAction test cases.

---
 .../test/Modules/cxx20-module-file-info-macros.cpp | 14 ++
 1 file changed, 14 insertions(+)

diff --git a/clang/test/Modules/cxx20-module-file-info-macros.cpp 
b/clang/test/Modules/cxx20-module-file-info-macros.cpp
index 3b67e9b9acd410..567d21cf1fa43d 100644
--- a/clang/test/Modules/cxx20-module-file-info-macros.cpp
+++ b/clang/test/Modules/cxx20-module-file-info-macros.cpp
@@ -40,6 +40,11 @@
 // CHECK-DAG: FUNC_Macro
 // CHECK-DAG: CONSTANT
 // CHECK-DAG: FOO
+// CHECK: Macro Definition Bodies:
+// CHECK-DAG: REDEFINE
+// CHECK-DAG: FUNC_Macro(X) (X+1)
+// CHECK-DAG: CONSTANT 43
+// CHECK-DAG: FOO
 // CHECK-NEXT: ===
 
 //--- include_foo.h
@@ -49,6 +54,10 @@
 // CHECK-DAG: CONSTANT
 // CHECK-DAG: FUNC_Macro
 // CHECK-DAG: FOO
+// CHECK: Macro Definition Bodies:
+// CHECK-DAG: FUNC_Macro(X) (X+1)
+// CHECK-DAG: CONSTANT 43
+// CHECK-DAG: FOO
 // CHECK-NEXT: ===
 
 //--- import_foo.h
@@ -58,6 +67,10 @@ import "foo.h";
 // CHECK-DAG: CONSTANT
 // CHECK-DAG: FUNC_Macro
 // CHECK-DAG: FOO
+// CHECK: Macro Definition Bodies:
+// CHECK-DAG: FUNC_Macro
+// CHECK-DAG: CONSTANT
+// CHECK-DAG: FOO
 // CHECK-NEXT: ===
 
 //--- named_module.cppm
@@ -66,3 +79,4 @@ module;
 export module M;
 #define M_Module 43
 // CHECK-NOT: Macro Definitions:
+// CHECK-NOT: Macro Definition Bodies:

>From ce3e5abfe2d994974d3a41b98f7e152e358afdb7 Mon Sep 17 00:00:00 2001
From: root 
Date: Sun, 21 Apr 2024 16:45:04 +0800
Subject: [PATCH 2/2] [clang][frontend] Make DumpModuleInfoAction emit the full
 macro.

---
 clang/lib/Frontend/FrontendActions.cpp | 61 ++
 1 file changed, 53 insertions(+), 8 deletions(-)

diff --git a/clang/lib/Frontend/FrontendActions.cpp 
b/clang/lib/Frontend/FrontendActions.cpp
index 04eb1041326713..3800958e34cca5 100644
--- a/clang/lib/Frontend/FrontendActions.cpp
+++ b/clang/lib/Frontend/FrontendActions.cpp
@@ -943,17 +943,62 @@ void DumpModuleInfoAction::ExecuteAction() {
   for (/* pair*/ const auto &Macro :
FilteredMacros)
 Out << " " << Macro.first->getName() << "\n";
-}
 
-// Now let's print out any modules we did not see as part of the Primary.
-for (const auto &SM : SubModMap) {
-  if (!SM.second.Seen && SM.second.Mod) {
-Out << "  " << ModuleKindName(SM.second.Kind) << " '" << SM.first
-<< "' at index #" << SM.second.Idx
-<< " has no direct reference in the Primary\n";
+  // Emit the macro definition bodies completely.
+  Out << "   Macro Definition Bodies:\n";
+  for (const auto &Macro : FilteredMacros) {
+Out << " " << Macro.first->getName();
+clang::MacroInfo *MI = PP.getMacroInfo(Macro.first);
+if (MI == nullptr) {
+  Out << '\n';
+  continue;
+}
+if (MI->isFunctionLike()) {
+  Out << '(';
+  if (!MI->param_empty()) {
+MacroInfo::param_iterator AI = MI->param_begin(),
+  E = MI->param_end();
+for (; AI + 1 != E; ++AI) {
+  Out << (*AI)->getName();
+  Out << ',';
+}
+
+// Last argument.
+if ((*AI)->getName() == "__VA_ARGS__")
+  Out << "...";
+else
+  Out << (*AI)->getName();
+  }
+
+  if (MI->isGNUVarargs())
+// #define foo(x...)
+Out << "...";
+
+  Out << ')';
+}
+
+SmallString<128> SpellingBuffer;
+bool First = true;
+for (const auto &T : MI->tokens()) {
+  if (First || T.hasLeadingSpace())
+Out << " ";
+  First = false;
+
+  Out << PP.getSpelling(T, SpellingBuffer);
+}
+Out << '\n';
+  }
+
+  // Now let's print out any modules we did not see as part of the Primary.
+  for (const auto &SM : SubModMap) {
+if (!SM.second.Seen && SM.second.Mod) {
+  Out << "  " << ModuleKindName(SM.second.Kind) << " '" << SM.first
+  << "' at index #" << SM.second.Idx
+  << " has no direct reference in the Primary\n";
+}
   }
+  Out << "  == ==\n";
 }
-Out << "  == ==\n";
   }
 
   // The reminder of the output is produced from the listener as the AST

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][frontend] Make DumpModuleInfoAction emit the full macro (PR #85745)

2024-04-21 Thread Zhang Yi via cfe-commits

zhanyi22333 wrote:

This commit use tokens from MacroInfo to emit the macro body.
It emits macro body below the part of macro name.

For import test case, it can not get MacroInfo. So it will just print macro 
name as below.

```
   Macro Definitions:
 CONSTANT
 FUNC_Macro
 FOO
   Macro Definition Bodies:
 CONSTANT
 FUNC_Macro
 FOO
```

 I wonder whether it is better to not print body part for import case.


https://github.com/llvm/llvm-project/pull/85745
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits