Skip to content

Commit 193e900

Browse files
committed
[OpenACC][NFC] Fix begin loc and split it from the directive location
I discovered while working on something else that we were using the location of the directive name as the 'beginloc' which caused some problems in a few places. This patch makes it so our beginloc is the '#' as we originally designed, and then adds a DirectiveLoc concept to a construct for use diagnosing the name.
1 parent 9e89d10 commit 193e900

File tree

9 files changed

+49
-37
lines changed

9 files changed

+49
-37
lines changed

clang/include/clang/AST/StmtOpenACC.h

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ class OpenACCConstructStmt : public Stmt {
3131
/// The location of the directive statement, from the '#' to the last token of
3232
/// the directive.
3333
SourceRange Range;
34+
/// The location of the directive name.
35+
SourceLocation DirectiveLoc;
3436

3537
/// The list of clauses. This is stored here as an ArrayRef, as this is the
3638
/// most convienient place to access the list, however the list itself should
@@ -39,8 +41,9 @@ class OpenACCConstructStmt : public Stmt {
3941

4042
protected:
4143
OpenACCConstructStmt(StmtClass SC, OpenACCDirectiveKind K,
42-
SourceLocation Start, SourceLocation End)
43-
: Stmt(SC), Kind(K), Range(Start, End) {}
44+
SourceLocation Start, SourceLocation DirectiveLoc,
45+
SourceLocation End)
46+
: Stmt(SC), Kind(K), Range(Start, End), DirectiveLoc(DirectiveLoc) {}
4447

4548
// Used only for initialization, the leaf class can initialize this to
4649
// trailing storage.
@@ -59,6 +62,7 @@ class OpenACCConstructStmt : public Stmt {
5962

6063
SourceLocation getBeginLoc() const { return Range.getBegin(); }
6164
SourceLocation getEndLoc() const { return Range.getEnd(); }
65+
SourceLocation getDirectiveLoc() const { return DirectiveLoc; }
6266
ArrayRef<const OpenACCClause *> clauses() const { return Clauses; }
6367

6468
child_range children() {
@@ -81,9 +85,11 @@ class OpenACCAssociatedStmtConstruct : public OpenACCConstructStmt {
8185

8286
protected:
8387
OpenACCAssociatedStmtConstruct(StmtClass SC, OpenACCDirectiveKind K,
84-
SourceLocation Start, SourceLocation End,
85-
Stmt *AssocStmt)
86-
: OpenACCConstructStmt(SC, K, Start, End), AssociatedStmt(AssocStmt) {}
88+
SourceLocation Start,
89+
SourceLocation DirectiveLoc,
90+
SourceLocation End, Stmt *AssocStmt)
91+
: OpenACCConstructStmt(SC, K, Start, DirectiveLoc, End),
92+
AssociatedStmt(AssocStmt) {}
8793

8894
void setAssociatedStmt(Stmt *S) { AssociatedStmt = S; }
8995
Stmt *getAssociatedStmt() { return AssociatedStmt; }
@@ -126,10 +132,10 @@ class OpenACCComputeConstruct final
126132
friend class ASTStmtReader;
127133
friend class ASTContext;
128134
OpenACCComputeConstruct(unsigned NumClauses)
129-
: OpenACCAssociatedStmtConstruct(OpenACCComputeConstructClass,
130-
OpenACCDirectiveKind::Invalid,
131-
SourceLocation{}, SourceLocation{},
132-
/*AssociatedStmt=*/nullptr) {
135+
: OpenACCAssociatedStmtConstruct(
136+
OpenACCComputeConstructClass, OpenACCDirectiveKind::Invalid,
137+
SourceLocation{}, SourceLocation{}, SourceLocation{},
138+
/*AssociatedStmt=*/nullptr) {
133139
// We cannot send the TrailingObjects storage to the base class (which holds
134140
// a reference to the data) until it is constructed, so we have to set it
135141
// separately here.
@@ -141,11 +147,11 @@ class OpenACCComputeConstruct final
141147
}
142148

143149
OpenACCComputeConstruct(OpenACCDirectiveKind K, SourceLocation Start,
144-
SourceLocation End,
150+
SourceLocation DirectiveLoc, SourceLocation End,
145151
ArrayRef<const OpenACCClause *> Clauses,
146152
Stmt *StructuredBlock)
147153
: OpenACCAssociatedStmtConstruct(OpenACCComputeConstructClass, K, Start,
148-
End, StructuredBlock) {
154+
DirectiveLoc, End, StructuredBlock) {
149155
assert(isOpenACCComputeDirectiveKind(K) &&
150156
"Only parallel, serial, and kernels constructs should be "
151157
"represented by this type");
@@ -169,8 +175,8 @@ class OpenACCComputeConstruct final
169175
unsigned NumClauses);
170176
static OpenACCComputeConstruct *
171177
Create(const ASTContext &C, OpenACCDirectiveKind K, SourceLocation BeginLoc,
172-
SourceLocation EndLoc, ArrayRef<const OpenACCClause *> Clauses,
173-
Stmt *StructuredBlock);
178+
SourceLocation DirectiveLoc, SourceLocation EndLoc,
179+
ArrayRef<const OpenACCClause *> Clauses, Stmt *StructuredBlock);
174180

175181
Stmt *getStructuredBlock() { return getAssociatedStmt(); }
176182
const Stmt *getStructuredBlock() const {

clang/include/clang/Parse/Parser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3659,6 +3659,7 @@ class Parser : public CodeCompletionHandler {
36593659
struct OpenACCDirectiveParseInfo {
36603660
OpenACCDirectiveKind DirKind;
36613661
SourceLocation StartLoc;
3662+
SourceLocation DirLoc;
36623663
SourceLocation EndLoc;
36633664
SmallVector<OpenACCClause *> Clauses;
36643665
// TODO OpenACC: As we implement support for the Atomic, Routine, Cache, and

clang/include/clang/Sema/SemaOpenACC.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ class SemaOpenACC : public SemaBase {
379379
/// Called after the construct has been parsed, but clauses haven't been
380380
/// parsed. This allows us to diagnose not-implemented, as well as set up any
381381
/// state required for parsing the clauses.
382-
void ActOnConstruct(OpenACCDirectiveKind K, SourceLocation StartLoc);
382+
void ActOnConstruct(OpenACCDirectiveKind K, SourceLocation DirLoc);
383383

384384
/// Called after the directive, including its clauses, have been parsed and
385385
/// parsing has consumed the 'annot_pragma_openacc_end' token. This DOES
@@ -400,6 +400,7 @@ class SemaOpenACC : public SemaBase {
400400
/// declaration group or associated statement.
401401
StmtResult ActOnEndStmtDirective(OpenACCDirectiveKind K,
402402
SourceLocation StartLoc,
403+
SourceLocation DirLoc,
403404
SourceLocation EndLoc,
404405
ArrayRef<OpenACCClause *> Clauses,
405406
StmtResult AssocStmt);

clang/lib/AST/StmtOpenACC.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,14 @@ OpenACCComputeConstruct::CreateEmpty(const ASTContext &C, unsigned NumClauses) {
2323
return Inst;
2424
}
2525

26-
OpenACCComputeConstruct *
27-
OpenACCComputeConstruct::Create(const ASTContext &C, OpenACCDirectiveKind K,
28-
SourceLocation BeginLoc, SourceLocation EndLoc,
29-
ArrayRef<const OpenACCClause *> Clauses,
30-
Stmt *StructuredBlock) {
26+
OpenACCComputeConstruct *OpenACCComputeConstruct::Create(
27+
const ASTContext &C, OpenACCDirectiveKind K, SourceLocation BeginLoc,
28+
SourceLocation DirLoc, SourceLocation EndLoc,
29+
ArrayRef<const OpenACCClause *> Clauses, Stmt *StructuredBlock) {
3130
void *Mem = C.Allocate(
3231
OpenACCComputeConstruct::totalSizeToAlloc<const OpenACCClause *>(
3332
Clauses.size()));
34-
auto *Inst = new (Mem)
35-
OpenACCComputeConstruct(K, BeginLoc, EndLoc, Clauses, StructuredBlock);
33+
auto *Inst = new (Mem) OpenACCComputeConstruct(K, BeginLoc, DirLoc, EndLoc,
34+
Clauses, StructuredBlock);
3635
return Inst;
3736
}

clang/lib/Parse/ParseOpenACC.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,11 +1347,13 @@ void Parser::ParseOpenACCCacheVarList() {
13471347
ParseOpenACCVarList(OpenACCClauseKind::Invalid);
13481348
}
13491349

1350-
Parser::OpenACCDirectiveParseInfo Parser::ParseOpenACCDirective() {
1351-
SourceLocation StartLoc = getCurToken().getLocation();
1350+
Parser::OpenACCDirectiveParseInfo
1351+
Parser::ParseOpenACCDirective() {
1352+
SourceLocation StartLoc = ConsumeAnnotationToken();
1353+
SourceLocation DirLoc = getCurToken().getLocation();
13521354
OpenACCDirectiveKind DirKind = ParseOpenACCDirectiveKind(*this);
13531355

1354-
getActions().OpenACC().ActOnConstruct(DirKind, StartLoc);
1356+
getActions().OpenACC().ActOnConstruct(DirKind, DirLoc);
13551357

13561358
// Once we've parsed the construct/directive name, some have additional
13571359
// specifiers that need to be taken care of. Atomic has an 'atomic-clause'
@@ -1390,7 +1392,7 @@ Parser::OpenACCDirectiveParseInfo Parser::ParseOpenACCDirective() {
13901392
break;
13911393
case OpenACCDirectiveKind::Wait:
13921394
// OpenACC has an optional paren-wrapped 'wait-argument'.
1393-
if (ParseOpenACCWaitArgument(StartLoc, /*IsDirective=*/true).Failed)
1395+
if (ParseOpenACCWaitArgument(DirLoc, /*IsDirective=*/true).Failed)
13941396
T.skipToEnd();
13951397
else
13961398
T.consumeClose();
@@ -1404,7 +1406,8 @@ Parser::OpenACCDirectiveParseInfo Parser::ParseOpenACCDirective() {
14041406
}
14051407

14061408
// Parses the list of clauses, if present, plus set up return value.
1407-
OpenACCDirectiveParseInfo ParseInfo{DirKind, StartLoc, SourceLocation{},
1409+
OpenACCDirectiveParseInfo ParseInfo{DirKind, StartLoc, DirLoc,
1410+
SourceLocation{},
14081411
ParseOpenACCClauseList(DirKind)};
14091412

14101413
assert(Tok.is(tok::annot_pragma_openacc_end) &&
@@ -1421,7 +1424,6 @@ Parser::DeclGroupPtrTy Parser::ParseOpenACCDirectiveDecl() {
14211424
assert(Tok.is(tok::annot_pragma_openacc) && "expected OpenACC Start Token");
14221425

14231426
ParsingOpenACCDirectiveRAII DirScope(*this);
1424-
ConsumeAnnotationToken();
14251427

14261428
OpenACCDirectiveParseInfo DirInfo = ParseOpenACCDirective();
14271429

@@ -1438,7 +1440,6 @@ StmtResult Parser::ParseOpenACCDirectiveStmt() {
14381440
assert(Tok.is(tok::annot_pragma_openacc) && "expected OpenACC Start Token");
14391441

14401442
ParsingOpenACCDirectiveRAII DirScope(*this);
1441-
ConsumeAnnotationToken();
14421443

14431444
OpenACCDirectiveParseInfo DirInfo = ParseOpenACCDirective();
14441445
if (getActions().OpenACC().ActOnStartStmtDirective(DirInfo.DirKind,
@@ -1456,6 +1457,6 @@ StmtResult Parser::ParseOpenACCDirectiveStmt() {
14561457
}
14571458

14581459
return getActions().OpenACC().ActOnEndStmtDirective(
1459-
DirInfo.DirKind, DirInfo.StartLoc, DirInfo.EndLoc, DirInfo.Clauses,
1460-
AssocStmt);
1460+
DirInfo.DirKind, DirInfo.StartLoc, DirInfo.DirLoc, DirInfo.EndLoc,
1461+
DirInfo.Clauses, AssocStmt);
14611462
}

clang/lib/Sema/SemaOpenACC.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ ExprResult SemaOpenACC::CheckReductionVar(Expr *VarExpr) {
844844
}
845845

846846
void SemaOpenACC::ActOnConstruct(OpenACCDirectiveKind K,
847-
SourceLocation StartLoc) {
847+
SourceLocation DirLoc) {
848848
switch (K) {
849849
case OpenACCDirectiveKind::Invalid:
850850
// Nothing to do here, an invalid kind has nothing we can check here. We
@@ -859,7 +859,7 @@ void SemaOpenACC::ActOnConstruct(OpenACCDirectiveKind K,
859859
// here as these constructs do not take any arguments.
860860
break;
861861
default:
862-
Diag(StartLoc, diag::warn_acc_construct_unimplemented) << K;
862+
Diag(DirLoc, diag::warn_acc_construct_unimplemented) << K;
863863
break;
864864
}
865865
}
@@ -1265,6 +1265,7 @@ bool SemaOpenACC::ActOnStartStmtDirective(OpenACCDirectiveKind K,
12651265

12661266
StmtResult SemaOpenACC::ActOnEndStmtDirective(OpenACCDirectiveKind K,
12671267
SourceLocation StartLoc,
1268+
SourceLocation DirLoc,
12681269
SourceLocation EndLoc,
12691270
ArrayRef<OpenACCClause *> Clauses,
12701271
StmtResult AssocStmt) {
@@ -1278,7 +1279,7 @@ StmtResult SemaOpenACC::ActOnEndStmtDirective(OpenACCDirectiveKind K,
12781279
case OpenACCDirectiveKind::Kernels:
12791280
// TODO OpenACC: Add clauses to the construct here.
12801281
return OpenACCComputeConstruct::Create(
1281-
getASTContext(), K, StartLoc, EndLoc, Clauses,
1282+
getASTContext(), K, StartLoc, DirLoc, EndLoc, Clauses,
12821283
AssocStmt.isUsable() ? AssocStmt.get() : nullptr);
12831284
}
12841285
llvm_unreachable("Unhandled case in directive handling?");

clang/lib/Sema/TreeTransform.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4033,11 +4033,12 @@ class TreeTransform {
40334033

40344034
StmtResult RebuildOpenACCComputeConstruct(OpenACCDirectiveKind K,
40354035
SourceLocation BeginLoc,
4036+
SourceLocation DirLoc,
40364037
SourceLocation EndLoc,
40374038
ArrayRef<OpenACCClause *> Clauses,
40384039
StmtResult StrBlock) {
4039-
return getSema().OpenACC().ActOnEndStmtDirective(K, BeginLoc, EndLoc,
4040-
Clauses, StrBlock);
4040+
return getSema().OpenACC().ActOnEndStmtDirective(K, BeginLoc, DirLoc,
4041+
EndLoc, Clauses, StrBlock);
40414042
}
40424043

40434044
private:
@@ -11559,8 +11560,8 @@ StmtResult TreeTransform<Derived>::TransformOpenACCComputeConstruct(
1155911560
getSema().OpenACC().ActOnAssociatedStmt(C->getDirectiveKind(), StrBlock);
1156011561

1156111562
return getDerived().RebuildOpenACCComputeConstruct(
11562-
C->getDirectiveKind(), C->getBeginLoc(), C->getEndLoc(),
11563-
TransformedClauses, StrBlock);
11563+
C->getDirectiveKind(), C->getBeginLoc(), C->getDirectiveLoc(),
11564+
C->getEndLoc(), TransformedClauses, StrBlock);
1156411565
}
1156511566

1156611567
//===----------------------------------------------------------------------===//

clang/lib/Serialization/ASTReaderStmt.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2797,6 +2797,7 @@ void ASTStmtReader::VisitOpenACCConstructStmt(OpenACCConstructStmt *S) {
27972797
(void)Record.readInt();
27982798
S->Kind = Record.readEnum<OpenACCDirectiveKind>();
27992799
S->Range = Record.readSourceRange();
2800+
S->DirectiveLoc = Record.readSourceLocation();
28002801
Record.readOpenACCClauseList(S->Clauses);
28012802
}
28022803

clang/lib/Serialization/ASTWriterStmt.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2847,6 +2847,7 @@ void ASTStmtWriter::VisitOpenACCConstructStmt(OpenACCConstructStmt *S) {
28472847
Record.push_back(S->clauses().size());
28482848
Record.writeEnum(S->Kind);
28492849
Record.AddSourceRange(S->Range);
2850+
Record.AddSourceLocation(S->DirectiveLoc);
28502851
Record.writeOpenACCClauseList(S->clauses());
28512852
}
28522853

0 commit comments

Comments
 (0)