Skip to content

Commit 26f8e14

Browse files
committed
[OpenACC] NFC: Stop using 'getSpelling' while parsing OpenACC
It was brought up during the cache review that we shouldn't be using 'getSpelling', and instead should use the IdentifierInfo itself. This patch replaces all uses of it.
1 parent 8d0fb9f commit 26f8e14

File tree

5 files changed

+64
-53
lines changed

5 files changed

+64
-53
lines changed

clang/include/clang/Basic/DiagnosticParseKinds.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1362,7 +1362,7 @@ def warn_pragma_acc_unimplemented_clause_parsing
13621362
: Warning<"OpenACC clause parsing not yet implemented">,
13631363
InGroup<SourceUsesOpenACC>;
13641364
def err_acc_invalid_directive
1365-
: Error<"invalid OpenACC directive '%select{%1|%1 %2}0'">;
1365+
: Error<"invalid OpenACC directive %select{%1|'%1 %2'}0">;
13661366
def err_acc_missing_directive : Error<"expected OpenACC directive">;
13671367
def err_acc_invalid_open_paren
13681368
: Error<"expected clause-list or newline in OpenACC directive">;

clang/lib/Parse/ParseOpenACC.cpp

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ enum class OpenACCDirectiveKindEx {
3636
// identifies the first token), and doesn't fully handle 'enter data', 'exit
3737
// data', nor any of the 'atomic' variants, just the first token of each. So
3838
// this should only be used by `ParseOpenACCDirectiveKind`.
39-
OpenACCDirectiveKindEx getOpenACCDirectiveKind(StringRef Name) {
39+
OpenACCDirectiveKindEx getOpenACCDirectiveKind(Token Tok) {
40+
if (!Tok.is(tok::identifier))
41+
return OpenACCDirectiveKindEx::Invalid;
4042
OpenACCDirectiveKind DirKind =
41-
llvm::StringSwitch<OpenACCDirectiveKind>(Name)
43+
llvm::StringSwitch<OpenACCDirectiveKind>(
44+
Tok.getIdentifierInfo()->getName())
4245
.Case("parallel", OpenACCDirectiveKind::Parallel)
4346
.Case("serial", OpenACCDirectiveKind::Serial)
4447
.Case("kernels", OpenACCDirectiveKind::Kernels)
@@ -58,39 +61,46 @@ OpenACCDirectiveKindEx getOpenACCDirectiveKind(StringRef Name) {
5861
if (DirKind != OpenACCDirectiveKind::Invalid)
5962
return static_cast<OpenACCDirectiveKindEx>(DirKind);
6063

61-
return llvm::StringSwitch<OpenACCDirectiveKindEx>(Name)
64+
return llvm::StringSwitch<OpenACCDirectiveKindEx>(
65+
Tok.getIdentifierInfo()->getName())
6266
.Case("enter", OpenACCDirectiveKindEx::Enter)
6367
.Case("exit", OpenACCDirectiveKindEx::Exit)
6468
.Default(OpenACCDirectiveKindEx::Invalid);
6569
}
6670

6771
// Since 'atomic' is effectively a compound directive, this will decode the
6872
// second part of the directive.
69-
OpenACCAtomicKind getOpenACCAtomicKind(StringRef Name) {
70-
return llvm::StringSwitch<OpenACCAtomicKind>(Name)
73+
OpenACCAtomicKind getOpenACCAtomicKind(Token Tok) {
74+
if (!Tok.is(tok::identifier))
75+
return OpenACCAtomicKind::Invalid;
76+
return llvm::StringSwitch<OpenACCAtomicKind>(
77+
Tok.getIdentifierInfo()->getName())
7178
.Case("read", OpenACCAtomicKind::Read)
7279
.Case("write", OpenACCAtomicKind::Write)
7380
.Case("update", OpenACCAtomicKind::Update)
7481
.Case("capture", OpenACCAtomicKind::Capture)
7582
.Default(OpenACCAtomicKind::Invalid);
7683
}
7784

78-
bool isOpenACCDirectiveKind(OpenACCDirectiveKind Kind, StringRef Tok) {
85+
bool isOpenACCDirectiveKind(OpenACCDirectiveKind Kind, Token Tok) {
86+
if (!Tok.is(tok::identifier))
87+
return false;
88+
7989
switch (Kind) {
8090
case OpenACCDirectiveKind::Parallel:
81-
return Tok == "parallel";
91+
return Tok.getIdentifierInfo()->isStr("parallel");
8292
case OpenACCDirectiveKind::Serial:
83-
return Tok == "serial";
93+
return Tok.getIdentifierInfo()->isStr("serial");
8494
case OpenACCDirectiveKind::Kernels:
85-
return Tok == "kernels";
95+
return Tok.getIdentifierInfo()->isStr("kernels");
8696
case OpenACCDirectiveKind::Data:
87-
return Tok == "data";
97+
return Tok.getIdentifierInfo()->isStr("data");
8898
case OpenACCDirectiveKind::HostData:
89-
return Tok == "host_data";
99+
return Tok.getIdentifierInfo()->isStr("host_data");
90100
case OpenACCDirectiveKind::Loop:
91-
return Tok == "loop";
101+
return Tok.getIdentifierInfo()->isStr("loop");
92102
case OpenACCDirectiveKind::Cache:
93-
return Tok == "cache";
103+
return Tok.getIdentifierInfo()->isStr("cache");
94104

95105
case OpenACCDirectiveKind::ParallelLoop:
96106
case OpenACCDirectiveKind::SerialLoop:
@@ -100,19 +110,19 @@ bool isOpenACCDirectiveKind(OpenACCDirectiveKind Kind, StringRef Tok) {
100110
return false;
101111

102112
case OpenACCDirectiveKind::Atomic:
103-
return Tok == "atomic";
113+
return Tok.getIdentifierInfo()->isStr("atomic");
104114
case OpenACCDirectiveKind::Routine:
105-
return Tok == "routine";
115+
return Tok.getIdentifierInfo()->isStr("routine");
106116
case OpenACCDirectiveKind::Declare:
107-
return Tok == "declare";
117+
return Tok.getIdentifierInfo()->isStr("declare");
108118
case OpenACCDirectiveKind::Init:
109-
return Tok == "init";
119+
return Tok.getIdentifierInfo()->isStr("init");
110120
case OpenACCDirectiveKind::Shutdown:
111-
return Tok == "shutdown";
121+
return Tok.getIdentifierInfo()->isStr("shutdown");
112122
case OpenACCDirectiveKind::Set:
113-
return Tok == "set";
123+
return Tok.getIdentifierInfo()->isStr("set");
114124
case OpenACCDirectiveKind::Update:
115-
return Tok == "update";
125+
return Tok.getIdentifierInfo()->isStr("update");
116126
case OpenACCDirectiveKind::Invalid:
117127
return false;
118128
}
@@ -121,20 +131,22 @@ bool isOpenACCDirectiveKind(OpenACCDirectiveKind Kind, StringRef Tok) {
121131

122132
OpenACCDirectiveKind
123133
ParseOpenACCEnterExitDataDirective(Parser &P, Token FirstTok,
124-
StringRef FirstTokSpelling,
125134
OpenACCDirectiveKindEx ExtDirKind) {
126135
Token SecondTok = P.getCurToken();
127136

128137
if (SecondTok.isAnnotation()) {
129-
P.Diag(FirstTok, diag::err_acc_invalid_directive) << 0 << FirstTokSpelling;
138+
P.Diag(FirstTok, diag::err_acc_invalid_directive)
139+
<< 0 << FirstTok.getIdentifierInfo();
130140
return OpenACCDirectiveKind::Invalid;
131141
}
132142

133-
std::string SecondTokSpelling = P.getPreprocessor().getSpelling(SecondTok);
134-
135-
if (!isOpenACCDirectiveKind(OpenACCDirectiveKind::Data, SecondTokSpelling)) {
136-
P.Diag(FirstTok, diag::err_acc_invalid_directive)
137-
<< 1 << FirstTokSpelling << SecondTokSpelling;
143+
if (!isOpenACCDirectiveKind(OpenACCDirectiveKind::Data, SecondTok)) {
144+
if (!SecondTok.is(tok::identifier))
145+
P.Diag(SecondTok, diag::err_expected) << tok::identifier;
146+
else
147+
P.Diag(FirstTok, diag::err_acc_invalid_directive)
148+
<< 1 << FirstTok.getIdentifierInfo()->getName()
149+
<< SecondTok.getIdentifierInfo()->getName();
138150
return OpenACCDirectiveKind::Invalid;
139151
}
140152

@@ -152,9 +164,7 @@ OpenACCAtomicKind ParseOpenACCAtomicKind(Parser &P) {
152164
if (AtomicClauseToken.isAnnotation())
153165
return OpenACCAtomicKind::Update;
154166

155-
std::string AtomicClauseSpelling =
156-
P.getPreprocessor().getSpelling(AtomicClauseToken);
157-
OpenACCAtomicKind AtomicKind = getOpenACCAtomicKind(AtomicClauseSpelling);
167+
OpenACCAtomicKind AtomicKind = getOpenACCAtomicKind(AtomicClauseToken);
158168

159169
// If we don't know what this is, treat it as 'nothing', and treat the rest of
160170
// this as a clause list, which, despite being invalid, is likely what the
@@ -178,9 +188,8 @@ OpenACCDirectiveKind ParseOpenACCDirectiveKind(Parser &P) {
178188
}
179189

180190
P.ConsumeToken();
181-
std::string FirstTokSpelling = P.getPreprocessor().getSpelling(FirstTok);
182191

183-
OpenACCDirectiveKindEx ExDirKind = getOpenACCDirectiveKind(FirstTokSpelling);
192+
OpenACCDirectiveKindEx ExDirKind = getOpenACCDirectiveKind(FirstTok);
184193

185194
// OpenACCDirectiveKindEx is meant to be an extended list
186195
// over OpenACCDirectiveKind, so any value below Invalid is one of the
@@ -190,14 +199,17 @@ OpenACCDirectiveKind ParseOpenACCDirectiveKind(Parser &P) {
190199
// immediately cast it and use it as that.
191200
if (ExDirKind >= OpenACCDirectiveKindEx::Invalid) {
192201
switch (ExDirKind) {
193-
case OpenACCDirectiveKindEx::Invalid:
194-
P.Diag(FirstTok, diag::err_acc_invalid_directive)
195-
<< 0 << FirstTokSpelling;
202+
case OpenACCDirectiveKindEx::Invalid: {
203+
if (!FirstTok.is(tok::identifier))
204+
P.Diag(FirstTok, diag::err_expected) << tok::identifier;
205+
else
206+
P.Diag(FirstTok, diag::err_acc_invalid_directive)
207+
<< 0 << FirstTok.getIdentifierInfo();
196208
return OpenACCDirectiveKind::Invalid;
209+
}
197210
case OpenACCDirectiveKindEx::Enter:
198211
case OpenACCDirectiveKindEx::Exit:
199-
return ParseOpenACCEnterExitDataDirective(P, FirstTok, FirstTokSpelling,
200-
ExDirKind);
212+
return ParseOpenACCEnterExitDataDirective(P, FirstTok, ExDirKind);
201213
}
202214
}
203215

@@ -208,8 +220,7 @@ OpenACCDirectiveKind ParseOpenACCDirectiveKind(Parser &P) {
208220
// clause.
209221
Token SecondTok = P.getCurToken();
210222
if (!SecondTok.isAnnotation() &&
211-
isOpenACCDirectiveKind(OpenACCDirectiveKind::Loop,
212-
P.getPreprocessor().getSpelling(SecondTok))) {
223+
isOpenACCDirectiveKind(OpenACCDirectiveKind::Loop, SecondTok)) {
213224
switch (DirKind) {
214225
default:
215226
// Nothing to do except in the below cases, as they should be diagnosed as

clang/test/ParserOpenACC/parse-constructs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void func() {
6161
// expected-warning@+1{{OpenACC directives not yet implemented, pragma ignored}}
6262
#pragma acc enter
6363
for(;;){}
64-
// expected-error@+3{{invalid OpenACC directive 'exit }'}}
64+
// expected-error@+3{{expected identifier}}
6565
// expected-warning@+2{{OpenACC clause parsing not yet implemented}}
6666
// expected-warning@+1{{OpenACC directives not yet implemented, pragma ignored}}
6767
#pragma acc exit }
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
// RUN: %clang_cc1 %s -verify -fopenacc
22

33
// Parser::ParseExternalDeclaration
4-
// expected-error@+3{{invalid OpenACC directive 'not'}}
4+
// expected-error@+3{{invalid OpenACC directive 'havent'}}
55
// expected-warning@+2{{OpenACC clause parsing not yet implemented}}
66
// expected-warning@+1{{OpenACC directives not yet implemented, pragma ignored}}
7-
#pragma acc not yet implemented
7+
#pragma acc havent implemented
88
int foo;
99

1010
struct S {
1111
// Parser::ParseStructUnionBody
12-
// expected-error@+3{{invalid OpenACC directive 'not'}}
12+
// expected-error@+3{{invalid OpenACC directive 'havent'}}
1313
// expected-warning@+2{{OpenACC clause parsing not yet implemented}}
1414
// expected-warning@+1{{OpenACC directives not yet implemented, pragma ignored}}
15-
#pragma acc not yet implemented
15+
#pragma acc havent implemented
1616
int foo;
1717
};
1818

1919
void func() {
2020
// Parser::ParseStmtOrDeclarationAfterAttributes
21-
// expected-error@+3{{invalid OpenACC directive 'not'}}
21+
// expected-error@+3{{invalid OpenACC directive 'havent'}}
2222
// expected-warning@+2{{OpenACC clause parsing not yet implemented}}
2323
// expected-warning@+1{{OpenACC directives not yet implemented, pragma ignored}}
24-
#pragma acc not yet implemented
24+
#pragma acc havent implemented
2525
while(0) {}
2626
}
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
// RUN: %clang_cc1 %s -verify -fopenacc
22

33
// Parser::ParseExternalDeclaration
4-
// expected-error@+3{{invalid OpenACC directive 'not'}}
4+
// expected-error@+3{{invalid OpenACC directive 'havent'}}
55
// expected-warning@+2{{OpenACC clause parsing not yet implemented}}
66
// expected-warning@+1{{OpenACC directives not yet implemented, pragma ignored}}
7-
#pragma acc not yet implemented
7+
#pragma acc havent implemented
88
int foo;
99

1010
struct S {
1111
// Parser::ParseCXXClassMemberDeclarationWithPragmas
12-
// expected-error@+3{{invalid OpenACC directive 'not'}}
12+
// expected-error@+3{{invalid OpenACC directive 'havent'}}
1313
// expected-warning@+2{{OpenACC clause parsing not yet implemented}}
1414
// expected-warning@+1{{OpenACC directives not yet implemented, pragma ignored}}
15-
#pragma acc not yet implemented
15+
#pragma acc havent implemented
1616
int foo;
1717
};
1818

1919
void func() {
2020
// Parser::ParseStmtOrDeclarationAfterAttributes
21-
// expected-error@+3{{invalid OpenACC directive 'not'}}
21+
// expected-error@+3{{invalid OpenACC directive 'havent'}}
2222
// expected-warning@+2{{OpenACC clause parsing not yet implemented}}
2323
// expected-warning@+1{{OpenACC directives not yet implemented, pragma ignored}}
24-
#pragma acc not yet implemented
24+
#pragma acc havent implemented
2525
while(false) {}
2626
}

0 commit comments

Comments
 (0)