Skip to content

Commit d039caa

Browse files
committed
support non-string-literals for the message clause
1 parent c79a1f1 commit d039caa

File tree

10 files changed

+802
-70
lines changed

10 files changed

+802
-70
lines changed

clang/include/clang/AST/OpenMPClause.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1874,6 +1874,10 @@ class OMPMessageClause final : public OMPClause {
18741874
// Expression of the 'message' clause.
18751875
Stmt *MessageString = nullptr;
18761876

1877+
// The message as a StringLiteral in case it is as string literal. This might
1878+
// be needed during compile time.
1879+
StringLiteral *MessageStringLiteral = nullptr;
1880+
18771881
/// Set message string of the clause.
18781882
void setMessageString(Expr *MS) { MessageString = MS; }
18791883

@@ -1887,10 +1891,14 @@ class OMPMessageClause final : public OMPClause {
18871891
/// \param StartLoc Starting location of the clause.
18881892
/// \param LParenLoc Location of '('.
18891893
/// \param EndLoc Ending location of the clause.
1894+
/// \param MessageStringLiteral The message as a StringLiteral in case it is
1895+
/// as string literal. This might be needed during compile time.
18901896
OMPMessageClause(Expr *MS, SourceLocation StartLoc, SourceLocation LParenLoc,
1891-
SourceLocation EndLoc)
1897+
SourceLocation EndLoc,
1898+
StringLiteral *MessageStringLiteral = nullptr)
18921899
: OMPClause(llvm::omp::OMPC_message, StartLoc, EndLoc),
1893-
LParenLoc(LParenLoc), MessageString(MS) {}
1900+
LParenLoc(LParenLoc), MessageString(MS),
1901+
MessageStringLiteral(MessageStringLiteral) {}
18941902

18951903
/// Build an empty clause.
18961904
OMPMessageClause()
@@ -1903,6 +1911,10 @@ class OMPMessageClause final : public OMPClause {
19031911
/// Returns message string of the clause.
19041912
Expr *getMessageString() const { return cast_or_null<Expr>(MessageString); }
19051913

1914+
// Returns the source message as a string literal in case it has been a string
1915+
// literal. Otherwise, return null.
1916+
StringLiteral *getAsStringLiteral() const { return MessageStringLiteral; }
1917+
19061918
child_range children() {
19071919
return child_range(&MessageString, &MessageString + 1);
19081920
}

clang/include/clang/Basic/DiagnosticParseKinds.td

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1499,8 +1499,10 @@ def err_omp_unexpected_directive : Error<
14991499
"unexpected OpenMP directive %select{|'#pragma omp %1'}0">;
15001500
def err_omp_expected_punc : Error<
15011501
"expected ',' or ')' in '%0' %select{clause|directive}1">;
1502-
def warn_clause_expected_string : Warning<
1502+
def warn_clause_expected_string_literal : Warning<
15031503
"expected string literal in 'clause %0' - ignoring">, InGroup<IgnoredPragmas>;
1504+
def warn_clause_expected_string: Warning<
1505+
"expected string in 'clause %0' - ignoring">, InGroup<IgnoredPragmas>;
15041506
def err_omp_unexpected_clause : Error<
15051507
"unexpected OpenMP clause '%0' in directive '#pragma omp %1'">;
15061508
def err_omp_unexpected_clause_extension_only : Error<

clang/lib/AST/OpenMPClause.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1963,8 +1963,12 @@ void OMPClausePrinter::VisitOMPSeverityClause(OMPSeverityClause *Node) {
19631963
}
19641964

19651965
void OMPClausePrinter::VisitOMPMessageClause(OMPMessageClause *Node) {
1966-
OS << "message(\""
1967-
<< cast<StringLiteral>(Node->getMessageString())->getString() << "\")";
1966+
OS << "message(";
1967+
if (StringLiteral *SL = Node->getAsStringLiteral())
1968+
OS << "\"" << SL->getString() << "\"";
1969+
else if (Expr *E = Node->getMessageString())
1970+
E->printPretty(OS, nullptr, Policy);
1971+
OS << ")";
19681972
}
19691973

19701974
void OMPClausePrinter::VisitOMPScheduleClause(OMPScheduleClause *Node) {

clang/lib/CodeGen/CGOpenMPRuntime.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2372,9 +2372,8 @@ void CGOpenMPRuntime::emitBarrierCall(CodeGenFunction &CGF, SourceLocation Loc,
23722372

23732373
void CGOpenMPRuntime::emitErrorCall(CodeGenFunction &CGF, SourceLocation Loc,
23742374
Expr *ME, bool IsFatal) {
2375-
llvm::Value *MVL =
2376-
ME ? CGF.EmitStringLiteralLValue(cast<StringLiteral>(ME)).getPointer(CGF)
2377-
: llvm::ConstantPointerNull::get(CGF.VoidPtrTy);
2375+
llvm::Value *MVL = ME ? CGF.EmitScalarExpr(ME)
2376+
: llvm::ConstantPointerNull::get(CGF.VoidPtrTy);
23782377
// Build call void __kmpc_error(ident_t *loc, int severity, const char
23792378
// *message)
23802379
llvm::Value *Args[] = {
@@ -2718,14 +2717,10 @@ void CGOpenMPRuntime::emitNumThreadsClause(
27182717
// as if sev-level is fatal."
27192718
Args.push_back(llvm::ConstantInt::get(
27202719
CGM.Int32Ty, Severity == OMPC_SEVERITY_warning ? 1 : 2));
2721-
if (Message) {
2722-
if (const StringLiteral *Msg = dyn_cast<StringLiteral>(Message))
2723-
Args.push_back(CGF.EmitStringLiteralLValue(Msg).getPointer(CGF));
2724-
else
2725-
Args.push_back(CGF.EmitScalarExpr(Message));
2726-
} else {
2720+
if (Message)
2721+
Args.push_back(CGF.EmitScalarExpr(Message));
2722+
else
27272723
Args.push_back(llvm::ConstantPointerNull::get(CGF.VoidPtrTy));
2728-
}
27292724
}
27302725
CGF.EmitRuntimeCall(
27312726
OMPBuilder.getOrCreateRuntimeFunction(CGM.getModule(), FnID), Args);

clang/lib/Sema/SemaOpenMP.cpp

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11082,15 +11082,19 @@ StmtResult SemaOpenMP::ActOnOpenMPErrorDirective(ArrayRef<OMPClause *> Clauses,
1108211082
OMPExecutableDirective::getSingleClause<OMPSeverityClause>(Clauses);
1108311083
const OMPMessageClause *MessageC =
1108411084
OMPExecutableDirective::getSingleClause<OMPMessageClause>(Clauses);
11085-
Expr *ME = MessageC ? MessageC->getMessageString() : nullptr;
1108611085

1108711086
if (!AtC || AtC->getAtKind() == OMPC_AT_compilation) {
11087+
StringLiteral *SL = MessageC ? MessageC->getAsStringLiteral() : nullptr;
11088+
if (MessageC && !SL)
11089+
Diag(MessageC->getMessageString()->getBeginLoc(),
11090+
diag::warn_clause_expected_string_literal)
11091+
<< getOpenMPClauseNameForDiag(OMPC_message);
1108811092
if (SeverityC && SeverityC->getSeverityKind() == OMPC_SEVERITY_warning)
1108911093
Diag(SeverityC->getSeverityKindKwLoc(), diag::warn_diagnose_if_succeeded)
11090-
<< (ME ? cast<StringLiteral>(ME)->getString() : "WARNING");
11094+
<< (SL ? SL->getString() : "WARNING");
1109111095
else
1109211096
Diag(StartLoc, diag::err_diagnose_if_succeeded)
11093-
<< (ME ? cast<StringLiteral>(ME)->getString() : "ERROR");
11097+
<< (SL ? SL->getString() : "ERROR");
1109411098
if (!SeverityC || SeverityC->getSeverityKind() != OMPC_SEVERITY_warning)
1109511099
return StmtError();
1109611100
}
@@ -16445,13 +16449,28 @@ OMPClause *SemaOpenMP::ActOnOpenMPMessageClause(Expr *ME,
1644516449
SourceLocation LParenLoc,
1644616450
SourceLocation EndLoc) {
1644716451
assert(ME && "NULL expr in Message clause");
16448-
if (!isa<StringLiteral>(ME)) {
16452+
QualType Type = ME->getType();
16453+
if ((!Type->isPointerType() && !Type->isArrayType()) ||
16454+
!Type->getPointeeOrArrayElementType()->isAnyCharacterType()) {
1644916455
Diag(ME->getBeginLoc(), diag::warn_clause_expected_string)
1645016456
<< getOpenMPClauseNameForDiag(OMPC_message);
1645116457
return nullptr;
1645216458
}
16459+
16460+
// If the string is a string literal, save it in case it is later needed
16461+
// during compile time. Also consider a const char pointer to a string
16462+
// literal. This is necessary for template instantiations where the string
16463+
// literal is not passed directly and we only get a const char pointer to it.
16464+
StringLiteral *SL = dyn_cast<StringLiteral>(
16465+
isa<ImplicitCastExpr>(ME) ? cast<ImplicitCastExpr>(ME)->getSubExpr()
16466+
: ME);
16467+
16468+
// Convert array type to pointer type if needed.
16469+
if (Type->isArrayType())
16470+
ME = SemaRef.DefaultFunctionArrayConversion(ME).get();
16471+
1645316472
return new (getASTContext())
16454-
OMPMessageClause(ME, StartLoc, LParenLoc, EndLoc);
16473+
OMPMessageClause(ME, StartLoc, LParenLoc, EndLoc, SL);
1645516474
}
1645616475

1645716476
OMPClause *SemaOpenMP::ActOnOpenMPOrderClause(

clang/lib/Sema/TreeTransform.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10948,8 +10948,7 @@ TreeTransform<Derived>::TransformOMPMessageClause(OMPMessageClause *C) {
1094810948
if (E.isInvalid())
1094910949
return nullptr;
1095010950
return getDerived().RebuildOMPMessageClause(
10951-
C->getMessageString(), C->getBeginLoc(), C->getLParenLoc(),
10952-
C->getEndLoc());
10951+
E.get(), C->getBeginLoc(), C->getLParenLoc(), C->getEndLoc());
1095310952
}
1095410953

1095510954
template <typename Derived>

0 commit comments

Comments
 (0)