[PATCH] D80409: [MS ABI] Add mangled type for auto template parameter whose argument kind is Integeral

2020-09-11 Thread Zequan Wu via Phabricator via cfe-commits
zequanwu closed this revision.
zequanwu added a comment.

committed here 
https://reviews.llvm.org/rG83286a1a8f059d1664b64341854676a36a85cecd.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D80409/new/

https://reviews.llvm.org/D80409

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


[PATCH] D80409: [MS ABI] Add mangled type for auto template parameter whose argument kind is Integeral

2020-09-11 Thread Zequan Wu via Phabricator via cfe-commits
zequanwu updated this revision to Diff 291355.
zequanwu added a comment.

update comment.
add a test case for template function.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D80409/new/

https://reviews.llvm.org/D80409

Files:
  clang/include/clang/Basic/LangOptions.h
  clang/lib/AST/MicrosoftMangle.cpp
  clang/test/CodeGenCXX/mangle-ms-auto-templates.cpp

Index: clang/test/CodeGenCXX/mangle-ms-auto-templates.cpp
===
--- /dev/null
+++ clang/test/CodeGenCXX/mangle-ms-auto-templates.cpp
@@ -0,0 +1,47 @@
+// RUN: %clang_cc1 -std=c++17 -fms-compatibility-version=19.20 -emit-llvm %s -o - -fms-extensions -fdelayed-template-parsing -triple=x86_64-pc-windows-msvc | FileCheck --check-prefix=AFTER %s
+// RUN: %clang_cc1 -std=c++17 -fms-compatibility-version=19.14 -emit-llvm %s -o - -fms-extensions -fdelayed-template-parsing -triple=x86_64-pc-windows-msvc | FileCheck --check-prefix=BEFORE %s
+
+template 
+class AutoParmTemplate {
+public:
+  AutoParmTemplate() {}
+};
+
+template 
+class AutoParmsTemplate {
+public:
+  AutoParmsTemplate() {}
+};
+
+template 
+auto AutoFunc () {
+  return a;
+}
+
+void template_mangling() {
+  AutoFunc<1>();
+  // AFTER: call {{.*}} @"??$AutoFunc@$MH00@@YA?A?@@XZ"
+  // BEFORE: call {{.*}} @"??$AutoFunc@$00@@YA?A?@@XZ"
+  AutoParmTemplate<0> auto_int;
+  // AFTER: call {{.*}} @"??0?$AutoParmTemplate@$MH0A@@@QEAA@XZ"
+  // BEFORE: call {{.*}} @"??0?$AutoParmTemplate@$0A@@@QEAA@XZ"
+  AutoParmTemplate<'a'> auto_char;
+  // AFTER: call {{.*}} @"??0?$AutoParmTemplate@$MD0GB@@@QEAA@XZ"
+  // BEFORE: call {{.*}} @"??0?$AutoParmTemplate@$0GB@@@QEAA@XZ"
+  AutoParmTemplate<9223372036854775807LL> int64_max;
+  // AFTER: call {{.*}} @"??0?$AutoParmTemplate@$M_J0HPPP@@@QEAA@XZ"
+  // BEFORE: call {{.*}} @"??0?$AutoParmTemplate@$0HPPP@@@QEAA@XZ"
+  AutoParmTemplate<-9223372036854775807LL - 1LL> int64_min;
+  // AFTER: call {{.*}} @"??0?$AutoParmTemplate@$M_J0?IAAA@@@QEAA@XZ"
+  // BEFORE: call {{.*}} @"??0?$AutoParmTemplate@$0?IAAA@@@QEAA@XZ"
+  AutoParmTemplate<(unsigned long long)-1> uint64_neg_1;
+  // AFTER: call {{.*}} @"??0?$AutoParmTemplate@$M_K0?0@@QEAA@XZ"
+  // BEFORE: call {{.*}} @"??0?$AutoParmTemplate@$0?0@@QEAA@XZ"
+
+  AutoParmsTemplate<0, false, 'a'> c1;
+  // AFTER: call {{.*}} @"??0?$AutoParmsTemplate@$MH0A@$M_N0A@$MD0GB@@@QEAA@XZ"
+  // BEFORE: call {{.*}} @"??0?$AutoParmsTemplate@$0A@$0A@$0GB@@@QEAA@XZ"
+  AutoParmsTemplate<(unsigned long)1, 9223372036854775807LL> c2;
+  // AFTER: call {{.*}} @"??0?$AutoParmsTemplate@$MK00$M_J0HPPP@@@QEAA@XZ"
+  // BEFORE: call {{.*}} @"??0?$AutoParmsTemplate@$00$0HPPP@@@QEAA@XZ"
+}
Index: clang/lib/AST/MicrosoftMangle.cpp
===
--- clang/lib/AST/MicrosoftMangle.cpp
+++ clang/lib/AST/MicrosoftMangle.cpp
@@ -378,8 +378,10 @@
   void mangleFunctionClass(const FunctionDecl *FD);
   void mangleCallingConvention(CallingConv CC);
   void mangleCallingConvention(const FunctionType *T);
-  void mangleIntegerLiteral(const llvm::APSInt &Number, bool IsBoolean);
-  void mangleExpression(const Expr *E);
+  void mangleIntegerLiteral(const llvm::APSInt &Number,
+const NonTypeTemplateParmDecl *PD = nullptr,
+QualType TemplateArgType = QualType());
+  void mangleExpression(const Expr *E, const NonTypeTemplateParmDecl *PD);
   void mangleThrowSpecification(const FunctionProtoType *T);
 
   void mangleTemplateArgs(const TemplateDecl *TD,
@@ -1357,24 +1359,36 @@
   mangleUnqualifiedName(TD);
 }
 
-void MicrosoftCXXNameMangler::mangleIntegerLiteral(const llvm::APSInt &Value,
-   bool IsBoolean) {
+void MicrosoftCXXNameMangler::mangleIntegerLiteral(
+const llvm::APSInt &Value, const NonTypeTemplateParmDecl *PD,
+QualType TemplateArgType) {
   //  ::= $0 
-  Out << "$0";
-  // Make sure booleans are encoded as 0/1.
-  if (IsBoolean && Value.getBoolValue())
-mangleNumber(1);
-  else if (Value.isSigned())
+  Out << "$";
+
+  // Since MSVC 2019, add 'M[]' after '$' for auto template parameter when
+  // argument is integer.
+  if (getASTContext().getLangOpts().isCompatibleWithMSVC(
+  LangOptions::MSVC2019) &&
+  PD && PD->getType()->getTypeClass() == Type::Auto &&
+  !TemplateArgType.isNull()) {
+Out << "M";
+mangleType(TemplateArgType, SourceRange(), QMM_Drop);
+  }
+
+  Out << "0";
+
+  if (Value.isSigned())
 mangleNumber(Value.getSExtValue());
   else
 mangleNumber(Value.getZExtValue());
 }
 
-void MicrosoftCXXNameMangler::mangleExpression(const Expr *E) {
+void MicrosoftCXXNameMangler::mangleExpression(
+const Expr *E, const NonTypeTemplateParmDecl *PD) {
   // See if this is a constant expression.
   if (Optional Value =
   E->getIntegerConstantExpr(Context.getAST

[PATCH] D80409: [MS ABI] Add mangled type for auto template parameter whose argument kind is Integeral

2020-09-11 Thread Reid "Away June-Sep" Kleckner via Phabricator via cfe-commits
rnk accepted this revision.
rnk added a comment.
This revision is now accepted and ready to land.

lgtm, thanks!


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D80409/new/

https://reviews.llvm.org/D80409

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


[PATCH] D80409: [MS ABI] Add mangled type for auto template parameter whose argument kind is Integeral

2020-09-11 Thread Zequan Wu via Phabricator via cfe-commits
zequanwu updated this revision to Diff 291331.
zequanwu added a comment.

Add check for MSVC version 19.14 (removed `AutoParmTemplate auto_bool` 
as its mangled name conflicts with `AutoParmTemplate<0> auto_int` in V19.14).


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D80409/new/

https://reviews.llvm.org/D80409

Files:
  clang/include/clang/Basic/LangOptions.h
  clang/lib/AST/MicrosoftMangle.cpp
  clang/test/CodeGenCXX/mangle-ms-auto-templates.cpp

Index: clang/test/CodeGenCXX/mangle-ms-auto-templates.cpp
===
--- /dev/null
+++ clang/test/CodeGenCXX/mangle-ms-auto-templates.cpp
@@ -0,0 +1,39 @@
+// RUN: %clang_cc1 -std=c++17 -fms-compatibility-version=19.20 -emit-llvm %s -o - -fms-extensions -fdelayed-template-parsing -triple=x86_64-pc-windows-msvc | FileCheck --check-prefix=AFTER %s
+// RUN: %clang_cc1 -std=c++17 -fms-compatibility-version=19.14 -emit-llvm %s -o - -fms-extensions -fdelayed-template-parsing -triple=x86_64-pc-windows-msvc | FileCheck --check-prefix=BEFORE %s
+
+template 
+class AutoParmTemplate {
+public:
+  AutoParmTemplate() {}
+};
+
+template 
+class AutoParmsTemplate {
+public:
+  AutoParmsTemplate() {}
+};
+
+void template_mangling() {
+  AutoParmTemplate<0> auto_int;
+  // AFTER: call {{.*}} @"??0?$AutoParmTemplate@$MH0A@@@QEAA@XZ"
+  // BEFORE: call {{.*}} @"??0?$AutoParmTemplate@$0A@@@QEAA@XZ"
+  AutoParmTemplate<'a'> auto_char;
+  // AFTER: call {{.*}} @"??0?$AutoParmTemplate@$MD0GB@@@QEAA@XZ"
+  // BEFORE: call {{.*}} @"??0?$AutoParmTemplate@$0GB@@@QEAA@XZ"
+  AutoParmTemplate<9223372036854775807LL> int64_max;
+  // AFTER: call {{.*}} @"??0?$AutoParmTemplate@$M_J0HPPP@@@QEAA@XZ"
+  // BEFORE: call {{.*}} @"??0?$AutoParmTemplate@$0HPPP@@@QEAA@XZ"
+  AutoParmTemplate<-9223372036854775807LL - 1LL> int64_min;
+  // AFTER: call {{.*}} @"??0?$AutoParmTemplate@$M_J0?IAAA@@@QEAA@XZ"
+  // BEFORE: call {{.*}} @"??0?$AutoParmTemplate@$0?IAAA@@@QEAA@XZ"
+  AutoParmTemplate<(unsigned long long)-1> uint64_neg_1;
+  // AFTER: call {{.*}} @"??0?$AutoParmTemplate@$M_K0?0@@QEAA@XZ"
+  // BEFORE: call {{.*}} @"??0?$AutoParmTemplate@$0?0@@QEAA@XZ"
+
+  AutoParmsTemplate<0, false, 'a'> c1;
+  // AFTER: call {{.*}} @"??0?$AutoParmsTemplate@$MH0A@$M_N0A@$MD0GB@@@QEAA@XZ"
+  // BEFORE: call {{.*}} @"??0?$AutoParmsTemplate@$0A@$0A@$0GB@@@QEAA@XZ"
+  AutoParmsTemplate<(unsigned long)1, 9223372036854775807LL> c2;
+  // AFTER: call {{.*}} @"??0?$AutoParmsTemplate@$MK00$M_J0HPPP@@@QEAA@XZ"
+  // BEFORE: call {{.*}} @"??0?$AutoParmsTemplate@$00$0HPPP@@@QEAA@XZ"
+}
Index: clang/lib/AST/MicrosoftMangle.cpp
===
--- clang/lib/AST/MicrosoftMangle.cpp
+++ clang/lib/AST/MicrosoftMangle.cpp
@@ -378,8 +378,10 @@
   void mangleFunctionClass(const FunctionDecl *FD);
   void mangleCallingConvention(CallingConv CC);
   void mangleCallingConvention(const FunctionType *T);
-  void mangleIntegerLiteral(const llvm::APSInt &Number, bool IsBoolean);
-  void mangleExpression(const Expr *E);
+  void mangleIntegerLiteral(const llvm::APSInt &Number,
+const NonTypeTemplateParmDecl *PD = nullptr,
+QualType TemplateArgType = QualType());
+  void mangleExpression(const Expr *E, const NonTypeTemplateParmDecl *PD);
   void mangleThrowSpecification(const FunctionProtoType *T);
 
   void mangleTemplateArgs(const TemplateDecl *TD,
@@ -1357,24 +1359,35 @@
   mangleUnqualifiedName(TD);
 }
 
-void MicrosoftCXXNameMangler::mangleIntegerLiteral(const llvm::APSInt &Value,
-   bool IsBoolean) {
-  //  ::= $0 
-  Out << "$0";
-  // Make sure booleans are encoded as 0/1.
-  if (IsBoolean && Value.getBoolValue())
-mangleNumber(1);
-  else if (Value.isSigned())
+void MicrosoftCXXNameMangler::mangleIntegerLiteral(
+const llvm::APSInt &Value, const NonTypeTemplateParmDecl *PD,
+QualType TemplateArgType) {
+  //  ::= $ [] 0 
+  Out << "$";
+
+  // mangle argument type if parmeter is auto
+  if (getASTContext().getLangOpts().isCompatibleWithMSVC(
+  LangOptions::MSVC2019) &&
+  PD && PD->getType()->getTypeClass() == Type::Auto &&
+  !TemplateArgType.isNull()) {
+Out << "M";
+mangleType(TemplateArgType, SourceRange(), QMM_Drop);
+  }
+
+  Out << "0";
+
+  if (Value.isSigned())
 mangleNumber(Value.getSExtValue());
   else
 mangleNumber(Value.getZExtValue());
 }
 
-void MicrosoftCXXNameMangler::mangleExpression(const Expr *E) {
+void MicrosoftCXXNameMangler::mangleExpression(
+const Expr *E, const NonTypeTemplateParmDecl *PD) {
   // See if this is a constant expression.
   if (Optional Value =
   E->getIntegerConstantExpr(Context.getASTContext())) {
-mangleIntegerLiteral(*Value, E->getType()->isBooleanType());
+mangleIntegerLiteral(*Value, PD, E->g

[PATCH] D80409: [MS ABI] Add mangled type for auto template parameter whose argument kind is Integeral

2020-09-11 Thread Reid "Away June-Sep" Kleckner via Phabricator via cfe-commits
rnk added a comment.

Looks good, but we should test both sides of the MSVC behavior.




Comment at: clang/test/CodeGenCXX/mangle-ms-auto-templates.cpp:17
+  AutoParmTemplate<0> auto_int;
+  // CHECK: call {{.*}} @"??0?$AutoParmTemplate@$H0A@@@QAE@XZ"
+  AutoParmTemplate auto_bool;

zequanwu wrote:
> thakis wrote:
> > zequanwu wrote:
> > > thakis wrote:
> > > > Are you sure this is correct? MSVC produces a different mangling 
> > > > (https://godbolt.org/z/VxRfJd) and neither `undname` nor `llvm-undname` 
> > > > / `demumble` can demangle the symbol here (while they demange the msvc 
> > > > output according to godbolt fine).
> > > I use `x64 msvc v19.24` version, which gives 
> > > `@"??0?$AutoParmTemplate@$MH0A@@@QAE@XZ"`. The extra `M` might come from 
> > > qualifier mangling. 
> > > 
> > > For `x86 msvc v19.24(WINE)` version, it produces 
> > > `??0?$AutoParmTemplate@$0A@@@QAE@XZ` for both `AutoParmTemplate<0> 
> > > auto_int` and `AutoParmTemplate auto_int`. Isn't this a bug?
> > The test at the top says -triple=i386. If you have x64 mangling results in 
> > here, you should use a 64-bit triple (ie -tripe=x86_64-pc-windows). If the 
> > mangling is structurally different for different bitnesses, we should 
> > probably have tests for both.
> > 
> > For symbols that can be exported, we need to match msvc's mangling to be 
> > abi-compatible, no matter if we consider the mangling a bug or not.
> In `x64 msvc v19.14`, it still gives the same mangling results as `x86`. From 
> godbolt, it looks like that the mangled type was only added since `msvc 
> v19.20`. (https://godbolt.org/z/xGT3w-). But the `MSVCMajorVersion` is only 
> up to 1914, which I guess means `v19.14`. 
> 
> I will add a new version for `v19.20` which will add `M[type]` for each auto 
> template parameter.
Please add a second RUN line with a new check prefix to test the behavior both 
before and after.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D80409/new/

https://reviews.llvm.org/D80409

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


[PATCH] D80409: [MS ABI] Add mangled type for auto template parameter whose argument kind is Integeral

2020-09-10 Thread Zequan Wu via Phabricator via cfe-commits
zequanwu updated this revision to Diff 29.
zequanwu marked 2 inline comments as done.
zequanwu retitled this revision from "[MS ABI] Add mangled type for auto 
template parameter whose argument kind is Integeral " to "[MS ABI] Add mangled 
type for auto template parameter whose argument kind is Integeral".
zequanwu added a comment.

rebase and address comment.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D80409/new/

https://reviews.llvm.org/D80409

Files:
  clang/include/clang/Basic/LangOptions.h
  clang/lib/AST/MicrosoftMangle.cpp
  clang/test/CodeGenCXX/mangle-ms-auto-templates.cpp

Index: clang/test/CodeGenCXX/mangle-ms-auto-templates.cpp
===
--- /dev/null
+++ clang/test/CodeGenCXX/mangle-ms-auto-templates.cpp
@@ -0,0 +1,33 @@
+// RUN: %clang_cc1 -std=c++17 -fms-compatibility-version=19.20 -emit-llvm %s -o - -fms-extensions -fdelayed-template-parsing -triple=x86_64-pc-windows-msvc | FileCheck %s
+
+template 
+class AutoParmTemplate {
+public:
+  AutoParmTemplate() {}
+};
+
+template 
+class AutoParmsTemplate {
+public:
+  AutoParmsTemplate() {}
+};
+
+void template_mangling() {
+  AutoParmTemplate<0> auto_int;
+  // CHECK: call {{.*}} @"??0?$AutoParmTemplate@$MH0A@@@QEAA@XZ"
+  AutoParmTemplate auto_bool;
+  // CHECK: call {{.*}} @"??0?$AutoParmTemplate@$M_N0A@@@QEAA@XZ"
+  AutoParmTemplate<'a'> auto_char;
+  // CHECK: call {{.*}} @"??0?$AutoParmTemplate@$MD0GB@@@QEAA@XZ"
+  AutoParmTemplate<9223372036854775807LL> int64_max;
+  // CHECK: call {{.*}} @"??0?$AutoParmTemplate@$M_J0HPPP@@@QEAA@XZ"
+  AutoParmTemplate<-9223372036854775807LL - 1LL> int64_min;
+  // CHECK: call {{.*}} @"??0?$AutoParmTemplate@$M_J0?IAAA@@@QEAA@XZ"
+  AutoParmTemplate<(unsigned long long)-1> uint64_neg_1;
+  // CHECK: call {{.*}} @"??0?$AutoParmTemplate@$M_K0?0@@QEAA@XZ"
+
+  AutoParmsTemplate<0, false, 'a'> c1;
+  // CHECK: call {{.*}} @"??0?$AutoParmsTemplate@$MH0A@$M_N0A@$MD0GB@@@QEAA@XZ"
+  AutoParmsTemplate<(unsigned long)1, 9223372036854775807LL> c2;
+  // CHECK: call {{.*}} @"??0?$AutoParmsTemplate@$MK00$M_J0HPPP@@@QEAA@XZ"
+}
Index: clang/lib/AST/MicrosoftMangle.cpp
===
--- clang/lib/AST/MicrosoftMangle.cpp
+++ clang/lib/AST/MicrosoftMangle.cpp
@@ -378,8 +378,10 @@
   void mangleFunctionClass(const FunctionDecl *FD);
   void mangleCallingConvention(CallingConv CC);
   void mangleCallingConvention(const FunctionType *T);
-  void mangleIntegerLiteral(const llvm::APSInt &Number, bool IsBoolean);
-  void mangleExpression(const Expr *E);
+  void mangleIntegerLiteral(const llvm::APSInt &Number,
+const NonTypeTemplateParmDecl *PD = nullptr,
+QualType TemplateArgType = QualType());
+  void mangleExpression(const Expr *E, const NonTypeTemplateParmDecl *PD);
   void mangleThrowSpecification(const FunctionProtoType *T);
 
   void mangleTemplateArgs(const TemplateDecl *TD,
@@ -1357,24 +1359,35 @@
   mangleUnqualifiedName(TD);
 }
 
-void MicrosoftCXXNameMangler::mangleIntegerLiteral(const llvm::APSInt &Value,
-   bool IsBoolean) {
-  //  ::= $0 
-  Out << "$0";
-  // Make sure booleans are encoded as 0/1.
-  if (IsBoolean && Value.getBoolValue())
-mangleNumber(1);
-  else if (Value.isSigned())
+void MicrosoftCXXNameMangler::mangleIntegerLiteral(
+const llvm::APSInt &Value, const NonTypeTemplateParmDecl *PD,
+QualType TemplateArgType) {
+  //  ::= $ [] 0 
+  Out << "$";
+
+  // mangle argument type if parmeter is auto
+  if (getASTContext().getLangOpts().isCompatibleWithMSVC(
+  LangOptions::MSVC2019) &&
+  PD && PD->getType()->getTypeClass() == Type::Auto &&
+  !TemplateArgType.isNull()) {
+Out << "M";
+mangleType(TemplateArgType, SourceRange(), QMM_Drop);
+  }
+
+  Out << "0";
+
+  if (Value.isSigned())
 mangleNumber(Value.getSExtValue());
   else
 mangleNumber(Value.getZExtValue());
 }
 
-void MicrosoftCXXNameMangler::mangleExpression(const Expr *E) {
+void MicrosoftCXXNameMangler::mangleExpression(
+const Expr *E, const NonTypeTemplateParmDecl *PD) {
   // See if this is a constant expression.
   if (Optional Value =
   E->getIntegerConstantExpr(Context.getASTContext())) {
-mangleIntegerLiteral(*Value, E->getType()->isBooleanType());
+mangleIntegerLiteral(*Value, PD, E->getType());
 return;
   }
 
@@ -1448,10 +1461,12 @@
 }
 break;
   }
-  case TemplateArgument::Integral:
+  case TemplateArgument::Integral: {
+QualType T = TA.getIntegralType();
 mangleIntegerLiteral(TA.getAsIntegral(),
- TA.getIntegralType()->isBooleanType());
+ cast(Parm), T);
 break;
+  }
   case TemplateArgument::NullPtr: {
 QualType T = TA.getNullPtrType();
 if (const MemberPointerType *MPT = T->getAs()

[PATCH] D80409: [MS ABI] Add mangled type for auto template parameter whose argument kind is Integeral

2020-09-10 Thread Reid "Away June-Sep" Kleckner via Phabricator via cfe-commits
rnk added inline comments.



Comment at: clang/lib/AST/MicrosoftMangle.cpp:383
+const NonTypeTemplateParmDecl *PD = nullptr,
+QualType *TemplateArgType = nullptr);
+  void mangleExpression(const Expr *E, const NonTypeTemplateParmDecl *PD);

I see you need an optional QualType here. Can you do this: `QualType 
TemplateArgType = QualType()`? I believe it will create a null QualType, which 
you can then check for with `.isNull()`.

QualType is pointer-sized and is generally passed by value. I think it will 
help make the other call sites shorter, so they don't need to take the address 
of a local variable.



Comment at: clang/lib/AST/MicrosoftMangle.cpp:1390
+QualType T = E->getType();
+mangleIntegerLiteral(Value, PD, &T);
 return;

For example, if you can avoid the pointer indirection, you can avoid the local 
variable here.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D80409/new/

https://reviews.llvm.org/D80409

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


[PATCH] D80409: [MS ABI] Add mangled type for auto template parameter whose argument kind is Integeral

2020-06-09 Thread Zequan Wu via Phabricator via cfe-commits
zequanwu added a comment.

Do you think adding `MSVC2019` to make the name mangling compatible with `MSVC 
v19.20` is acceptable?


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D80409/new/

https://reviews.llvm.org/D80409



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


[PATCH] D80409: [MS ABI] Add mangled type for auto template parameter whose argument kind is Integeral

2020-05-26 Thread Zequan Wu via Phabricator via cfe-commits
zequanwu updated this revision to Diff 266339.
zequanwu added a comment.

Add `19.20` version. Only add mangled type for version 19. 20 or later to be 
abi-compatible with https://godbolt.org/z/Bhc__A


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D80409/new/

https://reviews.llvm.org/D80409

Files:
  clang/include/clang/Basic/LangOptions.h
  clang/lib/AST/MicrosoftMangle.cpp
  clang/test/CodeGenCXX/mangle-ms-auto-templates.cpp

Index: clang/test/CodeGenCXX/mangle-ms-auto-templates.cpp
===
--- /dev/null
+++ clang/test/CodeGenCXX/mangle-ms-auto-templates.cpp
@@ -0,0 +1,33 @@
+// RUN: %clang_cc1 -std=c++17 -fms-compatibility-version=19.20 -emit-llvm %s -o - -fms-extensions -fdelayed-template-parsing -triple=x86_64-pc-windows-msvc | FileCheck %s
+
+template 
+class AutoParmTemplate {
+public:
+  AutoParmTemplate() {}
+};
+
+template 
+class AutoParmsTemplate {
+public:
+  AutoParmsTemplate() {}
+};
+
+void template_mangling() {
+  AutoParmTemplate<0> auto_int;
+  // CHECK: call {{.*}} @"??0?$AutoParmTemplate@$MH0A@@@QEAA@XZ"
+  AutoParmTemplate auto_bool;
+  // CHECK: call {{.*}} @"??0?$AutoParmTemplate@$M_N0A@@@QEAA@XZ"
+  AutoParmTemplate<'a'> auto_char;
+  // CHECK: call {{.*}} @"??0?$AutoParmTemplate@$MD0GB@@@QEAA@XZ"
+  AutoParmTemplate<9223372036854775807LL> int64_max;
+  // CHECK: call {{.*}} @"??0?$AutoParmTemplate@$M_J0HPPP@@@QEAA@XZ"
+  AutoParmTemplate<-9223372036854775807LL - 1LL> int64_min;
+  // CHECK: call {{.*}} @"??0?$AutoParmTemplate@$M_J0?IAAA@@@QEAA@XZ"
+  AutoParmTemplate<(unsigned long long)-1> uint64_neg_1;
+  // CHECK: call {{.*}} @"??0?$AutoParmTemplate@$M_K0?0@@QEAA@XZ"
+
+  AutoParmsTemplate<0, false, 'a'> c1;
+  // CHECK: call {{.*}} @"??0?$AutoParmsTemplate@$MH0A@$M_N0A@$MD0GB@@@QEAA@XZ"
+  AutoParmsTemplate<(unsigned long)1, 9223372036854775807LL> c2;
+  // CHECK: call {{.*}} @"??0?$AutoParmsTemplate@$MK00$M_J0HPPP@@@QEAA@XZ"
+}
Index: clang/lib/AST/MicrosoftMangle.cpp
===
--- clang/lib/AST/MicrosoftMangle.cpp
+++ clang/lib/AST/MicrosoftMangle.cpp
@@ -378,8 +378,10 @@
   void mangleFunctionClass(const FunctionDecl *FD);
   void mangleCallingConvention(CallingConv CC);
   void mangleCallingConvention(const FunctionType *T);
-  void mangleIntegerLiteral(const llvm::APSInt &Number, bool IsBoolean);
-  void mangleExpression(const Expr *E);
+  void mangleIntegerLiteral(const llvm::APSInt &Number,
+const NonTypeTemplateParmDecl *PD = nullptr,
+QualType *TemplateArgType = nullptr);
+  void mangleExpression(const Expr *E, const NonTypeTemplateParmDecl *PD);
   void mangleThrowSpecification(const FunctionProtoType *T);
 
   void mangleTemplateArgs(const TemplateDecl *TD,
@@ -1357,24 +1359,35 @@
   mangleUnqualifiedName(TD);
 }
 
-void MicrosoftCXXNameMangler::mangleIntegerLiteral(const llvm::APSInt &Value,
-   bool IsBoolean) {
-  //  ::= $0 
-  Out << "$0";
-  // Make sure booleans are encoded as 0/1.
-  if (IsBoolean && Value.getBoolValue())
-mangleNumber(1);
-  else if (Value.isSigned())
+void MicrosoftCXXNameMangler::mangleIntegerLiteral(
+const llvm::APSInt &Value, const NonTypeTemplateParmDecl *PD,
+QualType *TemplateArgType) {
+  //  ::= $ [] 0 
+  Out << "$";
+
+  // mangle argument type if parmeter is auto
+  if (getASTContext().getLangOpts().isCompatibleWithMSVC(
+  LangOptions::MSVC2019) &&
+  PD && PD->getType()->getTypeClass() == Type::Auto && TemplateArgType) {
+Out << "M";
+mangleType(*TemplateArgType, SourceRange(), QMM_Drop);
+  }
+
+  Out << "0";
+
+  if (Value.isSigned())
 mangleNumber(Value.getSExtValue());
   else
 mangleNumber(Value.getZExtValue());
 }
 
-void MicrosoftCXXNameMangler::mangleExpression(const Expr *E) {
+void MicrosoftCXXNameMangler::mangleExpression(
+const Expr *E, const NonTypeTemplateParmDecl *PD) {
   // See if this is a constant expression.
   llvm::APSInt Value;
   if (E->isIntegerConstantExpr(Value, Context.getASTContext())) {
-mangleIntegerLiteral(Value, E->getType()->isBooleanType());
+QualType T = E->getType();
+mangleIntegerLiteral(Value, PD, &T);
 return;
   }
 
@@ -1448,10 +1461,12 @@
 }
 break;
   }
-  case TemplateArgument::Integral:
+  case TemplateArgument::Integral: {
+QualType T = TA.getIntegralType();
 mangleIntegerLiteral(TA.getAsIntegral(),
- TA.getIntegralType()->isBooleanType());
+ cast(Parm), &T);
 break;
+  }
   case TemplateArgument::NullPtr: {
 QualType T = TA.getNullPtrType();
 if (const MemberPointerType *MPT = T->getAs()) {
@@ -1473,16 +1488,18 @@
 // However, we are free to use 0 *if* we would use multiple fields for
 // non-nullptr member pointers.
 if (!RD->nullFieldOffsetIsZero()) {
- 

[PATCH] D80409: [MS ABI] Add mangled type for auto template parameter whose argument kind is Integeral

2020-05-26 Thread Zequan Wu via Phabricator via cfe-commits
zequanwu marked an inline comment as done.
zequanwu added inline comments.



Comment at: clang/test/CodeGenCXX/mangle-ms-auto-templates.cpp:17
+  AutoParmTemplate<0> auto_int;
+  // CHECK: call {{.*}} @"??0?$AutoParmTemplate@$H0A@@@QAE@XZ"
+  AutoParmTemplate auto_bool;

thakis wrote:
> zequanwu wrote:
> > thakis wrote:
> > > Are you sure this is correct? MSVC produces a different mangling 
> > > (https://godbolt.org/z/VxRfJd) and neither `undname` nor `llvm-undname` / 
> > > `demumble` can demangle the symbol here (while they demange the msvc 
> > > output according to godbolt fine).
> > I use `x64 msvc v19.24` version, which gives 
> > `@"??0?$AutoParmTemplate@$MH0A@@@QAE@XZ"`. The extra `M` might come from 
> > qualifier mangling. 
> > 
> > For `x86 msvc v19.24(WINE)` version, it produces 
> > `??0?$AutoParmTemplate@$0A@@@QAE@XZ` for both `AutoParmTemplate<0> 
> > auto_int` and `AutoParmTemplate auto_int`. Isn't this a bug?
> The test at the top says -triple=i386. If you have x64 mangling results in 
> here, you should use a 64-bit triple (ie -tripe=x86_64-pc-windows). If the 
> mangling is structurally different for different bitnesses, we should 
> probably have tests for both.
> 
> For symbols that can be exported, we need to match msvc's mangling to be 
> abi-compatible, no matter if we consider the mangling a bug or not.
In `x64 msvc v19.14`, it still gives the same mangling results as `x86`. From 
godbolt, it looks like that the mangled type was only added since `msvc 
v19.20`. (https://godbolt.org/z/xGT3w-). But the `MSVCMajorVersion` is only up 
to 1914, which I guess means `v19.14`. 

I will add a new version for `v19.20` which will add `M[type]` for each auto 
template parameter.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D80409/new/

https://reviews.llvm.org/D80409



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


[PATCH] D80409: [MS ABI] Add mangled type for auto template parameter whose argument kind is Integeral

2020-05-26 Thread Nico Weber via Phabricator via cfe-commits
thakis added inline comments.



Comment at: clang/test/CodeGenCXX/mangle-ms-auto-templates.cpp:17
+  AutoParmTemplate<0> auto_int;
+  // CHECK: call {{.*}} @"??0?$AutoParmTemplate@$H0A@@@QAE@XZ"
+  AutoParmTemplate auto_bool;

zequanwu wrote:
> thakis wrote:
> > Are you sure this is correct? MSVC produces a different mangling 
> > (https://godbolt.org/z/VxRfJd) and neither `undname` nor `llvm-undname` / 
> > `demumble` can demangle the symbol here (while they demange the msvc output 
> > according to godbolt fine).
> I use `x64 msvc v19.24` version, which gives 
> `@"??0?$AutoParmTemplate@$MH0A@@@QAE@XZ"`. The extra `M` might come from 
> qualifier mangling. 
> 
> For `x86 msvc v19.24(WINE)` version, it produces 
> `??0?$AutoParmTemplate@$0A@@@QAE@XZ` for both `AutoParmTemplate<0> auto_int` 
> and `AutoParmTemplate auto_int`. Isn't this a bug?
The test at the top says -triple=i386. If you have x64 mangling results in 
here, you should use a 64-bit triple (ie -tripe=x86_64-pc-windows). If the 
mangling is structurally different for different bitnesses, we should probably 
have tests for both.

For symbols that can be exported, we need to match msvc's mangling to be 
abi-compatible, no matter if we consider the mangling a bug or not.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D80409/new/

https://reviews.llvm.org/D80409



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


[PATCH] D80409: [MS ABI] Add mangled type for auto template parameter whose argument kind is Integeral

2020-05-26 Thread Zequan Wu via Phabricator via cfe-commits
zequanwu marked an inline comment as done.
zequanwu added inline comments.



Comment at: clang/test/CodeGenCXX/mangle-ms-auto-templates.cpp:17
+  AutoParmTemplate<0> auto_int;
+  // CHECK: call {{.*}} @"??0?$AutoParmTemplate@$H0A@@@QAE@XZ"
+  AutoParmTemplate auto_bool;

thakis wrote:
> Are you sure this is correct? MSVC produces a different mangling 
> (https://godbolt.org/z/VxRfJd) and neither `undname` nor `llvm-undname` / 
> `demumble` can demangle the symbol here (while they demange the msvc output 
> according to godbolt fine).
I use `x64 msvc v19.24` version, which gives 
`@"??0?$AutoParmTemplate@$MH0A@@@QAE@XZ"`. The extra `M` might come from 
qualifier mangling. 

For `x86 msvc v19.24(WINE)` version, it produces 
`??0?$AutoParmTemplate@$0A@@@QAE@XZ` for both `AutoParmTemplate<0> auto_int` 
and `AutoParmTemplate auto_int`. Isn't this a bug?


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D80409/new/

https://reviews.llvm.org/D80409



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


[PATCH] D80409: [MS ABI] Add mangled type for auto template parameter whose argument kind is Integeral

2020-05-25 Thread Nico Weber via Phabricator via cfe-commits
thakis added inline comments.



Comment at: clang/test/CodeGenCXX/mangle-ms-auto-templates.cpp:17
+  AutoParmTemplate<0> auto_int;
+  // CHECK: call {{.*}} @"??0?$AutoParmTemplate@$H0A@@@QAE@XZ"
+  AutoParmTemplate auto_bool;

Are you sure this is correct? MSVC produces a different mangling 
(https://godbolt.org/z/VxRfJd) and neither `undname` nor `llvm-undname` / 
`demumble` can demangle the symbol here (while they demange the msvc output 
according to godbolt fine).


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D80409/new/

https://reviews.llvm.org/D80409



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


[PATCH] D80409: [MS ABI] Add mangled type for auto template parameter whose argument kind is Integeral

2020-05-25 Thread Zequan Wu via Phabricator via cfe-commits
zequanwu updated this revision to Diff 266082.
zequanwu retitled this revision from "[MS ABI] Add mangled type for template 
integer argument" to "[MS ABI] Add mangled type for auto template parameter 
whose argument kind is Integeral ".
zequanwu edited the summary of this revision.
zequanwu added a comment.

- Only add mangled type for auto template parameter, whose argument kind is 
`Integral`.
- MSVC mangled name has 1 more letter `M` before the mangled type, but I don't 
do it here because I think the `M` is from mangled qualifier and the function 
`mangleQualifiers` is incomplete yet.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D80409/new/

https://reviews.llvm.org/D80409

Files:
  clang/lib/AST/MicrosoftMangle.cpp
  clang/test/CodeGenCXX/mangle-ms-auto-templates.cpp

Index: clang/test/CodeGenCXX/mangle-ms-auto-templates.cpp
===
--- /dev/null
+++ clang/test/CodeGenCXX/mangle-ms-auto-templates.cpp
@@ -0,0 +1,33 @@
+// RUN: %clang_cc1 -std=c++17 -fms-compatibility-version=19 -emit-llvm %s -o - -fms-extensions -fdelayed-template-parsing -triple=i386-pc-win32 | FileCheck %s
+
+template 
+class AutoParmTemplate {
+public:
+  AutoParmTemplate() {}
+};
+
+template 
+class AutoParmsTemplate {
+public:
+  AutoParmsTemplate() {}
+};
+
+void template_mangling() {
+  AutoParmTemplate<0> auto_int;
+  // CHECK: call {{.*}} @"??0?$AutoParmTemplate@$H0A@@@QAE@XZ"
+  AutoParmTemplate auto_bool;
+  // CHECK: call {{.*}} @"??0?$AutoParmTemplate@$_N0A@@@QAE@XZ"
+  AutoParmTemplate<'a'> auto_char;
+  // CHECK: call {{.*}} @"??0?$AutoParmTemplate@$D0GB@@@QAE@XZ"
+  AutoParmTemplate<9223372036854775807LL> int64_max;
+  // CHECK: call {{.*}} @"??0?$AutoParmTemplate@$_J0HPPP@@@QAE@XZ"
+  AutoParmTemplate<-9223372036854775807LL - 1LL> int64_min;
+  // CHECK: call {{.*}} @"??0?$AutoParmTemplate@$_J0?IAAA@@@QAE@XZ"
+  AutoParmTemplate<(unsigned long long)-1> uint64_neg_1;
+  // CHECK: call {{.*}} @"??0?$AutoParmTemplate@$_K0?0@@QAE@XZ"
+
+  AutoParmsTemplate<0, false, 'a'> c1;
+  // CHECK: call {{.*}} @"??0?$AutoParmsTemplate@$H0A@$_N0A@$D0GB@@@QAE@XZ"
+  AutoParmsTemplate<(unsigned long)1, 9223372036854775807LL> c2;
+  // CHECK: call {{.*}} @"??0?$AutoParmsTemplate@$K00$_J0HPPP@@@QAE@XZ"
+}
Index: clang/lib/AST/MicrosoftMangle.cpp
===
--- clang/lib/AST/MicrosoftMangle.cpp
+++ clang/lib/AST/MicrosoftMangle.cpp
@@ -378,8 +378,10 @@
   void mangleFunctionClass(const FunctionDecl *FD);
   void mangleCallingConvention(CallingConv CC);
   void mangleCallingConvention(const FunctionType *T);
-  void mangleIntegerLiteral(const llvm::APSInt &Number, bool IsBoolean);
-  void mangleExpression(const Expr *E);
+  void mangleIntegerLiteral(const llvm::APSInt &Number,
+const NonTypeTemplateParmDecl *PD = nullptr,
+QualType *TemplateArgType = nullptr);
+  void mangleExpression(const Expr *E, const NonTypeTemplateParmDecl *PD);
   void mangleThrowSpecification(const FunctionProtoType *T);
 
   void mangleTemplateArgs(const TemplateDecl *TD,
@@ -1357,24 +1359,32 @@
   mangleUnqualifiedName(TD);
 }
 
-void MicrosoftCXXNameMangler::mangleIntegerLiteral(const llvm::APSInt &Value,
-   bool IsBoolean) {
-  //  ::= $0 
-  Out << "$0";
-  // Make sure booleans are encoded as 0/1.
-  if (IsBoolean && Value.getBoolValue())
-mangleNumber(1);
-  else if (Value.isSigned())
+void MicrosoftCXXNameMangler::mangleIntegerLiteral(
+const llvm::APSInt &Value, const NonTypeTemplateParmDecl *PD,
+QualType *TemplateArgType) {
+  //  ::= $ [] 0 
+  Out << "$";
+
+  // mangle argument type if parmeter is auto
+  if (PD && PD->getType()->getTypeClass() == Type::Auto && TemplateArgType) {
+mangleType(*TemplateArgType, SourceRange(), QMM_Drop);
+  }
+  
+  Out << "0";
+
+  if (Value.isSigned())
 mangleNumber(Value.getSExtValue());
   else
 mangleNumber(Value.getZExtValue());
 }
 
-void MicrosoftCXXNameMangler::mangleExpression(const Expr *E) {
+void MicrosoftCXXNameMangler::mangleExpression(
+const Expr *E, const NonTypeTemplateParmDecl *PD) {
   // See if this is a constant expression.
   llvm::APSInt Value;
   if (E->isIntegerConstantExpr(Value, Context.getASTContext())) {
-mangleIntegerLiteral(Value, E->getType()->isBooleanType());
+QualType T = E->getType();
+mangleIntegerLiteral(Value, PD, &T);
 return;
   }
 
@@ -1448,10 +1458,12 @@
 }
 break;
   }
-  case TemplateArgument::Integral:
+  case TemplateArgument::Integral: {
+QualType T = TA.getIntegralType();
 mangleIntegerLiteral(TA.getAsIntegral(),
- TA.getIntegralType()->isBooleanType());
+ cast(Parm), &T);
 break;
+  }
   case TemplateArgument::NullPtr: {
 QualType T = TA.getNullPtrType();
 if (const MemberPointerType *MPT