Skip to content

Commit 5c76ae2

Browse files
authored
[Clang] Support constexpr asm at global scope. (#143268)
I previously failed to realize this feature existed... Fixes #137459 Fixes #143242
1 parent 3906451 commit 5c76ae2

File tree

9 files changed

+32
-24
lines changed

9 files changed

+32
-24
lines changed

clang/include/clang/AST/ASTNodeTraverser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ class ASTNodeTraverser
605605
}
606606

607607
void VisitFileScopeAsmDecl(const FileScopeAsmDecl *D) {
608-
Visit(D->getAsmString());
608+
Visit(D->getAsmStringExpr());
609609
}
610610

611611
void VisitTopLevelStmtDecl(const TopLevelStmtDecl *D) { Visit(D->getStmt()); }

clang/include/clang/AST/Decl.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4490,18 +4490,18 @@ class RecordDecl : public TagDecl {
44904490
};
44914491

44924492
class FileScopeAsmDecl : public Decl {
4493-
StringLiteral *AsmString;
4493+
Expr *AsmString;
44944494
SourceLocation RParenLoc;
44954495

4496-
FileScopeAsmDecl(DeclContext *DC, StringLiteral *asmstring,
4497-
SourceLocation StartL, SourceLocation EndL)
4498-
: Decl(FileScopeAsm, DC, StartL), AsmString(asmstring), RParenLoc(EndL) {}
4496+
FileScopeAsmDecl(DeclContext *DC, Expr *asmstring, SourceLocation StartL,
4497+
SourceLocation EndL)
4498+
: Decl(FileScopeAsm, DC, StartL), AsmString(asmstring), RParenLoc(EndL) {}
44994499

45004500
virtual void anchor();
45014501

45024502
public:
4503-
static FileScopeAsmDecl *Create(ASTContext &C, DeclContext *DC,
4504-
StringLiteral *Str, SourceLocation AsmLoc,
4503+
static FileScopeAsmDecl *Create(ASTContext &C, DeclContext *DC, Expr *Str,
4504+
SourceLocation AsmLoc,
45054505
SourceLocation RParenLoc);
45064506

45074507
static FileScopeAsmDecl *CreateDeserialized(ASTContext &C, GlobalDeclID ID);
@@ -4513,9 +4513,11 @@ class FileScopeAsmDecl : public Decl {
45134513
return SourceRange(getAsmLoc(), getRParenLoc());
45144514
}
45154515

4516-
const StringLiteral *getAsmString() const { return AsmString; }
4517-
StringLiteral *getAsmString() { return AsmString; }
4518-
void setAsmString(StringLiteral *Asm) { AsmString = Asm; }
4516+
const Expr *getAsmStringExpr() const { return AsmString; }
4517+
Expr *getAsmStringExpr() { return AsmString; }
4518+
void setAsmString(Expr *Asm) { AsmString = Asm; }
4519+
4520+
std::string getAsmString() const;
45194521

45204522
static bool classof(const Decl *D) { return classofKind(D->getKind()); }
45214523
static bool classofKind(Kind K) { return K == FileScopeAsm; }

clang/include/clang/AST/RecursiveASTVisitor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1617,7 +1617,7 @@ DEF_TRAVERSE_DECL(LifetimeExtendedTemporaryDecl, {
16171617
})
16181618

16191619
DEF_TRAVERSE_DECL(FileScopeAsmDecl,
1620-
{ TRY_TO(TraverseStmt(D->getAsmString())); })
1620+
{ TRY_TO(TraverseStmt(D->getAsmStringExpr())); })
16211621

16221622
DEF_TRAVERSE_DECL(TopLevelStmtDecl, { TRY_TO(TraverseStmt(D->getStmt())); })
16231623

clang/lib/AST/Decl.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5713,8 +5713,7 @@ SourceRange TypeAliasDecl::getSourceRange() const {
57135713
void FileScopeAsmDecl::anchor() {}
57145714

57155715
FileScopeAsmDecl *FileScopeAsmDecl::Create(ASTContext &C, DeclContext *DC,
5716-
StringLiteral *Str,
5717-
SourceLocation AsmLoc,
5716+
Expr *Str, SourceLocation AsmLoc,
57185717
SourceLocation RParenLoc) {
57195718
return new (C, DC) FileScopeAsmDecl(DC, Str, AsmLoc, RParenLoc);
57205719
}
@@ -5725,6 +5724,10 @@ FileScopeAsmDecl *FileScopeAsmDecl::CreateDeserialized(ASTContext &C,
57255724
SourceLocation());
57265725
}
57275726

5727+
std::string FileScopeAsmDecl::getAsmString() const {
5728+
return GCCAsmStmt::ExtractStringFromGCCAsmStmtComponent(getAsmStringExpr());
5729+
}
5730+
57285731
void TopLevelStmtDecl::anchor() {}
57295732

57305733
TopLevelStmtDecl *TopLevelStmtDecl::Create(ASTContext &C, Stmt *Statement) {

clang/lib/AST/DeclPrinter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,8 +1042,8 @@ void DeclPrinter::VisitParmVarDecl(ParmVarDecl *D) {
10421042

10431043
void DeclPrinter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
10441044
Out << "__asm (";
1045-
D->getAsmString()->printPretty(Out, nullptr, Policy, Indentation, "\n",
1046-
&Context);
1045+
D->getAsmStringExpr()->printPretty(Out, nullptr, Policy, Indentation, "\n",
1046+
&Context);
10471047
Out << ")";
10481048
}
10491049

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7271,7 +7271,7 @@ void CodeGenModule::EmitTopLevelDecl(Decl *D) {
72717271
if (LangOpts.SYCLIsDevice)
72727272
break;
72737273
auto *AD = cast<FileScopeAsmDecl>(D);
7274-
getModule().appendModuleInlineAsm(AD->getAsmString()->getString());
7274+
getModule().appendModuleInlineAsm(AD->getAsmString());
72757275
break;
72767276
}
72777277

clang/lib/Sema/SemaDecl.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20514,14 +20514,11 @@ void Sema::ActOnEnumBody(SourceLocation EnumLoc, SourceRange BraceRange,
2051420514
CheckAlignasUnderalignment(Enum);
2051520515
}
2051620516

20517-
Decl *Sema::ActOnFileScopeAsmDecl(Expr *expr,
20518-
SourceLocation StartLoc,
20517+
Decl *Sema::ActOnFileScopeAsmDecl(Expr *expr, SourceLocation StartLoc,
2051920518
SourceLocation EndLoc) {
20520-
StringLiteral *AsmString = cast<StringLiteral>(expr);
2052120519

20522-
FileScopeAsmDecl *New = FileScopeAsmDecl::Create(Context, CurContext,
20523-
AsmString, StartLoc,
20524-
EndLoc);
20520+
FileScopeAsmDecl *New =
20521+
FileScopeAsmDecl::Create(Context, CurContext, expr, StartLoc, EndLoc);
2052520522
CurContext->addDecl(New);
2052620523
return New;
2052720524
}

clang/lib/Serialization/ASTWriterDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1389,7 +1389,7 @@ void ASTDeclWriter::VisitBindingDecl(BindingDecl *D) {
13891389

13901390
void ASTDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
13911391
VisitDecl(D);
1392-
Record.AddStmt(D->getAsmString());
1392+
Record.AddStmt(D->getAsmStringExpr());
13931393
Record.AddSourceLocation(D->getRParenLoc());
13941394
Code = serialization::DECL_FILE_SCOPE_ASM;
13951395
}

clang/test/CodeGenCXX/gnu-asm-constexpr.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ struct string_view {
1616
}
1717
};
1818

19+
namespace GH143242 {
20+
constexpr string_view code2 = R"(nop; nop; nop; nop)";
21+
asm((code2));
22+
// CHECK: module asm "nop; nop; nop; nop"
23+
}
24+
1925
int func() {return 0;};
2026

2127
void f() {
@@ -49,4 +55,4 @@ void test_srcloc() {
4955
foobar)o")
5056

5157
) ::(string_view("r"))(func()));
52-
}
58+
}

0 commit comments

Comments
 (0)