From: Samuel Antao Date: Mon, 15 Jun 2015 23:44:27 +0000 (+0000) Subject: According to the OpenMP spec, all the preprocessor macros should be X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2519ab75ea6b22a3815eef82ae51c9b1a690a272;p=clang According to the OpenMP spec, all the preprocessor macros should be expanded in OpenMP pragmas. This patch adds support for that in -E. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@239784 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Frontend/PrintPreprocessedOutput.cpp b/lib/Frontend/PrintPreprocessedOutput.cpp index 6192554299..a58c935620 100644 --- a/lib/Frontend/PrintPreprocessedOutput.cpp +++ b/lib/Frontend/PrintPreprocessedOutput.cpp @@ -562,8 +562,13 @@ struct UnknownPragmaHandler : public PragmaHandler { const char *Prefix; PrintPPOutputPPCallbacks *Callbacks; - UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks) - : Prefix(prefix), Callbacks(callbacks) {} + // Set to true if tokens should be expanded + bool ShouldExpandTokens; + + UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks, + bool RequireTokenExpansion) + : Prefix(prefix), Callbacks(callbacks), + ShouldExpandTokens(RequireTokenExpansion) {} void HandlePragma(Preprocessor &PP, PragmaIntroducerKind Introducer, Token &PragmaTok) override { // Figure out what line we went to and insert the appropriate number of @@ -571,16 +576,24 @@ struct UnknownPragmaHandler : public PragmaHandler { Callbacks->startNewLineIfNeeded(); Callbacks->MoveToLine(PragmaTok.getLocation()); Callbacks->OS.write(Prefix, strlen(Prefix)); + + Token PrevToken; + Token PrevPrevToken; + PrevToken.startToken(); + PrevPrevToken.startToken(); + // Read and print all of the pragma tokens. while (PragmaTok.isNot(tok::eod)) { - if (PragmaTok.hasLeadingSpace()) + if (PragmaTok.hasLeadingSpace() || + Callbacks->AvoidConcat(PrevPrevToken, PrevToken, PragmaTok)) Callbacks->OS << ' '; std::string TokSpell = PP.getSpelling(PragmaTok); Callbacks->OS.write(&TokSpell[0], TokSpell.size()); - // Expand macros in pragmas with -fms-extensions. The assumption is that - // the majority of pragmas in such a file will be Microsoft pragmas. - if (PP.getLangOpts().MicrosoftExt) + PrevPrevToken = PrevToken; + PrevToken = PragmaTok; + + if (ShouldExpandTokens) PP.Lex(PragmaTok); else PP.LexUnexpandedToken(PragmaTok); @@ -718,10 +731,29 @@ void clang::DoPrintPreprocessedInput(Preprocessor &PP, raw_ostream *OS, PrintPPOutputPPCallbacks *Callbacks = new PrintPPOutputPPCallbacks( PP, *OS, !Opts.ShowLineMarkers, Opts.ShowMacros, Opts.UseLineDirectives); - PP.AddPragmaHandler(new UnknownPragmaHandler("#pragma", Callbacks)); - PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC",Callbacks)); - PP.AddPragmaHandler("clang", - new UnknownPragmaHandler("#pragma clang", Callbacks)); + + // Expand macros in pragmas with -fms-extensions. The assumption is that + // the majority of pragmas in such a file will be Microsoft pragmas. + PP.AddPragmaHandler(new UnknownPragmaHandler( + "#pragma", Callbacks, + /*RequireTokenExpansion=*/PP.getLangOpts().MicrosoftExt)); + PP.AddPragmaHandler( + "GCC", new UnknownPragmaHandler( + "#pragma GCC", Callbacks, + /*RequireTokenExpansion=*/PP.getLangOpts().MicrosoftExt)); + PP.AddPragmaHandler( + "clang", new UnknownPragmaHandler( + "#pragma clang", Callbacks, + /*RequireTokenExpansion=*/PP.getLangOpts().MicrosoftExt)); + + // The tokens after pragma omp need to be expanded. + // + // OpenMP [2.1, Directive format] + // Preprocessing tokens following the #pragma omp are subject to macro + // replacement. + PP.AddPragmaHandler("omp", + new UnknownPragmaHandler("#pragma omp", Callbacks, + /*RequireTokenExpansion=*/true)); PP.addPPCallbacks(std::unique_ptr(Callbacks)); diff --git a/test/Preprocessor/openmp-macro-expansion.c b/test/Preprocessor/openmp-macro-expansion.c new file mode 100644 index 0000000000..a83512b592 --- /dev/null +++ b/test/Preprocessor/openmp-macro-expansion.c @@ -0,0 +1,31 @@ +// RUN: %clang_cc1 -fopenmp -E -o - %s 2>&1 | FileCheck %s + +// This is to make sure the pragma name is not expanded! +#define omp (0xDEADBEEF) + +#define N 2 +#define M 1 +#define E N> + +#define map_to_be_expanded(x) map(tofrom:x) +#define sched_to_be_expanded(x,s) schedule(x,s) +#define reda_to_be_expanded(x) reduction(+:x) +#define redb_to_be_expanded(x,op) reduction(op:x) + +void foo(int *a, int *b) { + //CHECK: omp target map(a[0:2]) map(tofrom:b[0:2*1]) + #pragma omp target map(a[0:N]) map_to_be_expanded(b[0:2*M]) + { + int reda; + int redb; + //CHECK: omp parallel for schedule(static,2> >1) reduction(+:reda) reduction(*:redb) + #pragma omp parallel for sched_to_be_expanded(static, E>1) \ + reda_to_be_expanded(reda) redb_to_be_expanded(redb,*) + for (int i = 0; i < N; ++i) { + reda += a[i]; + redb += b[i]; + } + a[0] = reda; + b[0] = redb; + } +}