Skip to content

Commit 1a1a11f

Browse files
authored
[clang][OpenMP] Issue a warning when parsing future directive spelling (#146933)
OpenMP 6.0 introduced alternative spelling for some directives, with the previous spellings still allowed. Warn the user when a new spelling is encountered with OpenMP version set to an older value.
1 parent ea62de5 commit 1a1a11f

File tree

4 files changed

+85
-5
lines changed

4 files changed

+85
-5
lines changed

clang/include/clang/Basic/DiagnosticGroups.td

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1530,9 +1530,11 @@ def OpenMPPre51Compat : DiagGroup<"pre-openmp-51-compat">;
15301530
def OpenMP51Ext : DiagGroup<"openmp-51-extensions">;
15311531
def OpenMPExtensions : DiagGroup<"openmp-extensions">;
15321532
def OpenMPTargetException : DiagGroup<"openmp-target-exception">;
1533+
def OpenMPFuture : DiagGroup<"openmp-future">;
15331534
def OpenMP : DiagGroup<"openmp", [
15341535
SourceUsesOpenMP, OpenMPClauses, OpenMPLoopForm, OpenMPTarget,
1535-
OpenMPMapping, OpenMP51Ext, OpenMPExtensions, OpenMPTargetException
1536+
OpenMPMapping, OpenMP51Ext, OpenMPExtensions, OpenMPTargetException,
1537+
OpenMPFuture
15361538
]>;
15371539

15381540
// OpenACC warnings.

clang/include/clang/Basic/DiagnosticParseKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,6 +1488,9 @@ def err_omp_multiple_step_or_linear_modifier : Error<
14881488
"multiple %select{'step size'|'linear modifier'}0 found in linear clause">;
14891489
def err_omp_deprecate_old_syntax: Error<
14901490
"old syntax '%0' on '%1' clause was deprecated, use new syntax '%2'">;
1491+
def warn_omp_future_directive_spelling: Warning<
1492+
"directive spelling '%0' is introduced in a later OpenMP version">,
1493+
InGroup<OpenMPFuture>;
14911494
def warn_pragma_expected_colon_r_paren : Warning<
14921495
"missing ':' or ')' after %0 - ignoring">, InGroup<IgnoredPragmas>;
14931496
def err_omp_unknown_directive : Error<

clang/lib/Parse/ParseOpenMP.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,21 @@ class DeclDirectiveListParserHelper final {
5656
};
5757
} // namespace
5858

59+
static OpenMPDirectiveKind checkOpenMPDirectiveName(Parser &P,
60+
SourceLocation Loc,
61+
OpenMPDirectiveKind Kind,
62+
StringRef Name) {
63+
unsigned Version = P.getLangOpts().OpenMP;
64+
auto [D, VR] = getOpenMPDirectiveKindAndVersions(Name);
65+
assert(D == Kind && "Directive kind mismatch");
66+
// Ignore the case Version > VR.Max: In OpenMP 6.0 all prior spellings
67+
// are explicitly allowed.
68+
if (Version < VR.Min)
69+
P.Diag(Loc, diag::warn_omp_future_directive_spelling) << Name;
70+
71+
return Kind;
72+
}
73+
5974
static OpenMPDirectiveKind parseOpenMPDirectiveKind(Parser &P) {
6075
static const DirectiveNameParser DirParser;
6176

@@ -65,23 +80,28 @@ static OpenMPDirectiveKind parseOpenMPDirectiveKind(Parser &P) {
6580
if (Tok.isAnnotation())
6681
return OMPD_unknown;
6782

68-
S = DirParser.consume(S, P.getPreprocessor().getSpelling(Tok));
83+
std::string Concat = P.getPreprocessor().getSpelling(Tok);
84+
SourceLocation Loc = Tok.getLocation();
85+
86+
S = DirParser.consume(S, Concat);
6987
if (S == nullptr)
7088
return OMPD_unknown;
7189

7290
while (!Tok.isAnnotation()) {
7391
OpenMPDirectiveKind DKind = S->Value;
7492
Tok = P.getPreprocessor().LookAhead(0);
7593
if (!Tok.isAnnotation()) {
76-
S = DirParser.consume(S, P.getPreprocessor().getSpelling(Tok));
94+
std::string TS = P.getPreprocessor().getSpelling(Tok);
95+
S = DirParser.consume(S, TS);
7796
if (S == nullptr)
78-
return DKind;
97+
return checkOpenMPDirectiveName(P, Loc, DKind, Concat);
98+
Concat += ' ' + TS;
7999
P.ConsumeToken();
80100
}
81101
}
82102

83103
assert(S && "Should have exited early");
84-
return S->Value;
104+
return checkOpenMPDirectiveName(P, Loc, S->Value, Concat);
85105
}
86106

87107
static DeclarationName parseOpenMPReductionId(Parser &P) {
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// RUN: %clang_cc1 -verify -fopenmp -fopenmp-version=52 -ferror-limit 100 -o - %s
2+
3+
// expected-warning@+1 {{directive spelling 'begin declare_target' is introduced in a later OpenMP version}}
4+
#pragma omp begin declare_target
5+
void f0();
6+
// expected-warning@+1 {{directive spelling 'end declare_target' is introduced in a later OpenMP version}}
7+
#pragma omp end declare_target
8+
9+
// expected-warning@+1 {{directive spelling 'begin declare_variant' is introduced in a later OpenMP version}}
10+
#pragma omp begin declare_variant match(user={condition(true)})
11+
void f1();
12+
// expected-warning@+1 {{directive spelling 'end declare_variant' is introduced in a later OpenMP version}}
13+
#pragma omp end declare_variant
14+
15+
int x;
16+
// expected-warning@+1 {{directive spelling 'declare_target' is introduced in a later OpenMP version}}
17+
#pragma omp declare_target(x)
18+
19+
struct A {
20+
int x, y;
21+
};
22+
// expected-warning@+1 {{directive spelling 'declare_mapper' is introduced in a later OpenMP version}}
23+
#pragma omp declare_mapper(mymapper: A a) map(tofrom:a.x, a.y)
24+
A add(A, A);
25+
// expected-warning@+1 {{directive spelling 'declare_reduction' is introduced in a later OpenMP version}}
26+
#pragma omp declare_reduction(+: A: omp_out = add(omp_in, omp_out))
27+
28+
// expected-warning@+1 {{directive spelling 'declare_simd' is introduced in a later OpenMP version}}
29+
#pragma omp declare_simd
30+
void f2();
31+
32+
void g3();
33+
// expected-warning@+1 {{directive spelling 'declare_variant' is introduced in a later OpenMP version}}
34+
#pragma omp declare_variant(g3) match(user={condition(true)})
35+
void f3() {}
36+
37+
void fred() {
38+
#pragma omp parallel
39+
{
40+
// expected-warning@+1 {{directive spelling 'cancellation_point' is introduced in a later OpenMP version}}
41+
#pragma omp cancellation_point parallel
42+
}
43+
44+
// expected-warning@+1 {{directive spelling 'target_data' is introduced in a later OpenMP version}}
45+
#pragma omp target_data map(tofrom: x)
46+
{}
47+
48+
// expected-warning@+1 {{directive spelling 'target_enter_data' is introduced in a later OpenMP version}}
49+
#pragma omp target_enter_data map(to: x)
50+
// expected-warning@+1 {{directive spelling 'target_exit_data' is introduced in a later OpenMP version}}
51+
#pragma omp target_exit_data map(from: x)
52+
// expected-warning@+1 {{directive spelling 'target_update' is introduced in a later OpenMP version}}
53+
#pragma omp target_update from(x)
54+
}
55+

0 commit comments

Comments
 (0)