-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[Clang] Support constexpr asm at global scope. #143268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
@llvm/pr-subscribers-clang-codegen @llvm/pr-subscribers-clang-modules Author: Corentin Jabot (cor3ntin) ChangesI previously failed to realize this feature existed... Fixes #137459 Full diff: https://github.com/llvm/llvm-project/pull/143268.diff 9 Files Affected:
diff --git a/clang/include/clang/AST/ASTNodeTraverser.h b/clang/include/clang/AST/ASTNodeTraverser.h
index 01bc12ce33eff..8d02a50e2e8a5 100644
--- a/clang/include/clang/AST/ASTNodeTraverser.h
+++ b/clang/include/clang/AST/ASTNodeTraverser.h
@@ -605,7 +605,7 @@ class ASTNodeTraverser
}
void VisitFileScopeAsmDecl(const FileScopeAsmDecl *D) {
- Visit(D->getAsmString());
+ Visit(D->getAsmStringExpr());
}
void VisitTopLevelStmtDecl(const TopLevelStmtDecl *D) { Visit(D->getStmt()); }
diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h
index 69ad18f4c0581..5c378e58cc1b9 100644
--- a/clang/include/clang/AST/Decl.h
+++ b/clang/include/clang/AST/Decl.h
@@ -4490,10 +4490,10 @@ class RecordDecl : public TagDecl {
};
class FileScopeAsmDecl : public Decl {
- StringLiteral *AsmString;
+ Expr *AsmString;
SourceLocation RParenLoc;
- FileScopeAsmDecl(DeclContext *DC, StringLiteral *asmstring,
+ FileScopeAsmDecl(DeclContext *DC, Expr *asmstring,
SourceLocation StartL, SourceLocation EndL)
: Decl(FileScopeAsm, DC, StartL), AsmString(asmstring), RParenLoc(EndL) {}
@@ -4501,7 +4501,7 @@ class FileScopeAsmDecl : public Decl {
public:
static FileScopeAsmDecl *Create(ASTContext &C, DeclContext *DC,
- StringLiteral *Str, SourceLocation AsmLoc,
+ Expr *Str, SourceLocation AsmLoc,
SourceLocation RParenLoc);
static FileScopeAsmDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
@@ -4513,9 +4513,12 @@ class FileScopeAsmDecl : public Decl {
return SourceRange(getAsmLoc(), getRParenLoc());
}
- const StringLiteral *getAsmString() const { return AsmString; }
- StringLiteral *getAsmString() { return AsmString; }
- void setAsmString(StringLiteral *Asm) { AsmString = Asm; }
+ const Expr *getAsmStringExpr() const { return AsmString; }
+ Expr *getAsmStringExpr() { return AsmString; }
+ void setAsmString(Expr *Asm) { AsmString = Asm; }
+
+ std::string getAsmString() const;
+
static bool classof(const Decl *D) { return classofKind(D->getKind()); }
static bool classofKind(Kind K) { return K == FileScopeAsm; }
diff --git a/clang/include/clang/AST/RecursiveASTVisitor.h b/clang/include/clang/AST/RecursiveASTVisitor.h
index a11157c006f92..b0f8ae621cf6d 100644
--- a/clang/include/clang/AST/RecursiveASTVisitor.h
+++ b/clang/include/clang/AST/RecursiveASTVisitor.h
@@ -1617,7 +1617,7 @@ DEF_TRAVERSE_DECL(LifetimeExtendedTemporaryDecl, {
})
DEF_TRAVERSE_DECL(FileScopeAsmDecl,
- { TRY_TO(TraverseStmt(D->getAsmString())); })
+ { TRY_TO(TraverseStmt(D->getAsmStringExpr())); })
DEF_TRAVERSE_DECL(TopLevelStmtDecl, { TRY_TO(TraverseStmt(D->getStmt())); })
diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp
index aad2d82401111..08d08293b3861 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -5713,7 +5713,7 @@ SourceRange TypeAliasDecl::getSourceRange() const {
void FileScopeAsmDecl::anchor() {}
FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
- StringLiteral *Str,
+ Expr *Str,
SourceLocation AsmLoc,
SourceLocation RParenLoc) {
return new (C, DC) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
@@ -5725,6 +5725,10 @@ FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C,
SourceLocation());
}
+std::string FileScopeAsmDecl::getAsmString() const {
+ return GCCAsmStmt::ExtractStringFromGCCAsmStmtComponent(getAsmStringExpr());
+}
+
void TopLevelStmtDecl::anchor() {}
TopLevelStmtDecl *TopLevelStmtDecl::Create(ASTContext &C, Stmt *Statement) {
diff --git a/clang/lib/AST/DeclPrinter.cpp b/clang/lib/AST/DeclPrinter.cpp
index 3616419dce4ec..be98b87d93cc5 100644
--- a/clang/lib/AST/DeclPrinter.cpp
+++ b/clang/lib/AST/DeclPrinter.cpp
@@ -1042,7 +1042,7 @@ void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
Out << "__asm (";
- D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation, "\n",
+ D->getAsmStringExpr()->printPretty(Out, nullptr, Policy, Indentation, "\n",
&Context);
Out << ")";
}
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index c005d3322ed7a..16e49aab4fe61 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -7271,7 +7271,7 @@ void CodeGenModule::EmitTopLevelDecl(Decl *D) {
if (LangOpts.SYCLIsDevice)
break;
auto *AD = cast<FileScopeAsmDecl>(D);
- getModule().appendModuleInlineAsm(AD->getAsmString()->getString());
+ getModule().appendModuleInlineAsm(AD->getAsmString());
break;
}
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 60e911b9fecc0..1bcc0c5a4fc4f 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -20517,10 +20517,9 @@ void Sema::ActOnEnumBody(SourceLocation EnumLoc, SourceRange BraceRange,
Decl *Sema::ActOnFileScopeAsmDecl(Expr *expr,
SourceLocation StartLoc,
SourceLocation EndLoc) {
- StringLiteral *AsmString = cast<StringLiteral>(expr);
- FileScopeAsmDecl *New = FileScopeAsmDecl::Create(Context, CurContext,
- AsmString, StartLoc,
+ FileScopeAsmDecl *New = FileScopeAsmDecl::Create(Context, CurContext,
+ expr, StartLoc,
EndLoc);
CurContext->addDecl(New);
return New;
diff --git a/clang/lib/Serialization/ASTWriterDecl.cpp b/clang/lib/Serialization/ASTWriterDecl.cpp
index b16026c4ba898..8f82324a27535 100644
--- a/clang/lib/Serialization/ASTWriterDecl.cpp
+++ b/clang/lib/Serialization/ASTWriterDecl.cpp
@@ -1389,7 +1389,7 @@ void ASTDeclWriter::VisitBindingDecl(BindingDecl *D) {
void ASTDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
VisitDecl(D);
- Record.AddStmt(D->getAsmString());
+ Record.AddStmt(D->getAsmStringExpr());
Record.AddSourceLocation(D->getRParenLoc());
Code = serialization::DECL_FILE_SCOPE_ASM;
}
diff --git a/clang/test/CodeGenCXX/gnu-asm-constexpr.cpp b/clang/test/CodeGenCXX/gnu-asm-constexpr.cpp
index d9c633ea69283..175a3b7bc588c 100644
--- a/clang/test/CodeGenCXX/gnu-asm-constexpr.cpp
+++ b/clang/test/CodeGenCXX/gnu-asm-constexpr.cpp
@@ -16,6 +16,12 @@ struct string_view {
}
};
+namespace GH143242 {
+ constexpr string_view code2 = R"(nop; nop; nop; nop)";
+ asm((code2));
+ // CHECK: module asm "nop; nop; nop; nop"
+}
+
int func() {return 0;};
void f() {
@@ -49,4 +55,4 @@ void test_srcloc() {
foobar)o")
) ::(string_view("r"))(func()));
- }
+}
|
|
@llvm/pr-subscribers-clang Author: Corentin Jabot (cor3ntin) ChangesI previously failed to realize this feature existed... Fixes #137459 Full diff: https://github.com/llvm/llvm-project/pull/143268.diff 9 Files Affected:
diff --git a/clang/include/clang/AST/ASTNodeTraverser.h b/clang/include/clang/AST/ASTNodeTraverser.h
index 01bc12ce33eff..8d02a50e2e8a5 100644
--- a/clang/include/clang/AST/ASTNodeTraverser.h
+++ b/clang/include/clang/AST/ASTNodeTraverser.h
@@ -605,7 +605,7 @@ class ASTNodeTraverser
}
void VisitFileScopeAsmDecl(const FileScopeAsmDecl *D) {
- Visit(D->getAsmString());
+ Visit(D->getAsmStringExpr());
}
void VisitTopLevelStmtDecl(const TopLevelStmtDecl *D) { Visit(D->getStmt()); }
diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h
index 69ad18f4c0581..5c378e58cc1b9 100644
--- a/clang/include/clang/AST/Decl.h
+++ b/clang/include/clang/AST/Decl.h
@@ -4490,10 +4490,10 @@ class RecordDecl : public TagDecl {
};
class FileScopeAsmDecl : public Decl {
- StringLiteral *AsmString;
+ Expr *AsmString;
SourceLocation RParenLoc;
- FileScopeAsmDecl(DeclContext *DC, StringLiteral *asmstring,
+ FileScopeAsmDecl(DeclContext *DC, Expr *asmstring,
SourceLocation StartL, SourceLocation EndL)
: Decl(FileScopeAsm, DC, StartL), AsmString(asmstring), RParenLoc(EndL) {}
@@ -4501,7 +4501,7 @@ class FileScopeAsmDecl : public Decl {
public:
static FileScopeAsmDecl *Create(ASTContext &C, DeclContext *DC,
- StringLiteral *Str, SourceLocation AsmLoc,
+ Expr *Str, SourceLocation AsmLoc,
SourceLocation RParenLoc);
static FileScopeAsmDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
@@ -4513,9 +4513,12 @@ class FileScopeAsmDecl : public Decl {
return SourceRange(getAsmLoc(), getRParenLoc());
}
- const StringLiteral *getAsmString() const { return AsmString; }
- StringLiteral *getAsmString() { return AsmString; }
- void setAsmString(StringLiteral *Asm) { AsmString = Asm; }
+ const Expr *getAsmStringExpr() const { return AsmString; }
+ Expr *getAsmStringExpr() { return AsmString; }
+ void setAsmString(Expr *Asm) { AsmString = Asm; }
+
+ std::string getAsmString() const;
+
static bool classof(const Decl *D) { return classofKind(D->getKind()); }
static bool classofKind(Kind K) { return K == FileScopeAsm; }
diff --git a/clang/include/clang/AST/RecursiveASTVisitor.h b/clang/include/clang/AST/RecursiveASTVisitor.h
index a11157c006f92..b0f8ae621cf6d 100644
--- a/clang/include/clang/AST/RecursiveASTVisitor.h
+++ b/clang/include/clang/AST/RecursiveASTVisitor.h
@@ -1617,7 +1617,7 @@ DEF_TRAVERSE_DECL(LifetimeExtendedTemporaryDecl, {
})
DEF_TRAVERSE_DECL(FileScopeAsmDecl,
- { TRY_TO(TraverseStmt(D->getAsmString())); })
+ { TRY_TO(TraverseStmt(D->getAsmStringExpr())); })
DEF_TRAVERSE_DECL(TopLevelStmtDecl, { TRY_TO(TraverseStmt(D->getStmt())); })
diff --git a/clang/lib/AST/Decl.cpp b/clang/lib/AST/Decl.cpp
index aad2d82401111..08d08293b3861 100644
--- a/clang/lib/AST/Decl.cpp
+++ b/clang/lib/AST/Decl.cpp
@@ -5713,7 +5713,7 @@ SourceRange TypeAliasDecl::getSourceRange() const {
void FileScopeAsmDecl::anchor() {}
FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
- StringLiteral *Str,
+ Expr *Str,
SourceLocation AsmLoc,
SourceLocation RParenLoc) {
return new (C, DC) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
@@ -5725,6 +5725,10 @@ FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C,
SourceLocation());
}
+std::string FileScopeAsmDecl::getAsmString() const {
+ return GCCAsmStmt::ExtractStringFromGCCAsmStmtComponent(getAsmStringExpr());
+}
+
void TopLevelStmtDecl::anchor() {}
TopLevelStmtDecl *TopLevelStmtDecl::Create(ASTContext &C, Stmt *Statement) {
diff --git a/clang/lib/AST/DeclPrinter.cpp b/clang/lib/AST/DeclPrinter.cpp
index 3616419dce4ec..be98b87d93cc5 100644
--- a/clang/lib/AST/DeclPrinter.cpp
+++ b/clang/lib/AST/DeclPrinter.cpp
@@ -1042,7 +1042,7 @@ void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
Out << "__asm (";
- D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation, "\n",
+ D->getAsmStringExpr()->printPretty(Out, nullptr, Policy, Indentation, "\n",
&Context);
Out << ")";
}
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index c005d3322ed7a..16e49aab4fe61 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -7271,7 +7271,7 @@ void CodeGenModule::EmitTopLevelDecl(Decl *D) {
if (LangOpts.SYCLIsDevice)
break;
auto *AD = cast<FileScopeAsmDecl>(D);
- getModule().appendModuleInlineAsm(AD->getAsmString()->getString());
+ getModule().appendModuleInlineAsm(AD->getAsmString());
break;
}
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index 60e911b9fecc0..1bcc0c5a4fc4f 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -20517,10 +20517,9 @@ void Sema::ActOnEnumBody(SourceLocation EnumLoc, SourceRange BraceRange,
Decl *Sema::ActOnFileScopeAsmDecl(Expr *expr,
SourceLocation StartLoc,
SourceLocation EndLoc) {
- StringLiteral *AsmString = cast<StringLiteral>(expr);
- FileScopeAsmDecl *New = FileScopeAsmDecl::Create(Context, CurContext,
- AsmString, StartLoc,
+ FileScopeAsmDecl *New = FileScopeAsmDecl::Create(Context, CurContext,
+ expr, StartLoc,
EndLoc);
CurContext->addDecl(New);
return New;
diff --git a/clang/lib/Serialization/ASTWriterDecl.cpp b/clang/lib/Serialization/ASTWriterDecl.cpp
index b16026c4ba898..8f82324a27535 100644
--- a/clang/lib/Serialization/ASTWriterDecl.cpp
+++ b/clang/lib/Serialization/ASTWriterDecl.cpp
@@ -1389,7 +1389,7 @@ void ASTDeclWriter::VisitBindingDecl(BindingDecl *D) {
void ASTDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
VisitDecl(D);
- Record.AddStmt(D->getAsmString());
+ Record.AddStmt(D->getAsmStringExpr());
Record.AddSourceLocation(D->getRParenLoc());
Code = serialization::DECL_FILE_SCOPE_ASM;
}
diff --git a/clang/test/CodeGenCXX/gnu-asm-constexpr.cpp b/clang/test/CodeGenCXX/gnu-asm-constexpr.cpp
index d9c633ea69283..175a3b7bc588c 100644
--- a/clang/test/CodeGenCXX/gnu-asm-constexpr.cpp
+++ b/clang/test/CodeGenCXX/gnu-asm-constexpr.cpp
@@ -16,6 +16,12 @@ struct string_view {
}
};
+namespace GH143242 {
+ constexpr string_view code2 = R"(nop; nop; nop; nop)";
+ asm((code2));
+ // CHECK: module asm "nop; nop; nop; nop"
+}
+
int func() {return 0;};
void f() {
@@ -49,4 +55,4 @@ void test_srcloc() {
foobar)o")
) ::(string_view("r"))(func()));
- }
+}
|
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
I previously failed to realize this feature existed... Fixes llvm#137459 Fixes llvm#143242
c09e49f to
e50565a
Compare
I previously failed to realize this feature existed... Fixes llvm#137459 Fixes llvm#143242
I previously failed to realize this feature existed...
Fixes #137459
Fixes #143242