r201821 - Expand macros in pragmas with -fms-extensions and -E

Reid Kleckner reid at kleckner.net
Thu Feb 20 14:59:51 PST 2014


Author: rnk
Date: Thu Feb 20 16:59:51 2014
New Revision: 201821

URL: http://llvm.org/viewvc/llvm-project?rev=201821&view=rev
Log:
Expand macros in pragmas with -fms-extensions and -E

gcc never expands macros in pragmas and MSVC always expands macros
before processing pragmas.  Clang usually allows macro expansion, except
in a handful of pragmas, most of which are handled by the lexer.

Also remove PPCallbacks for pragmas that are currently handled in the
parser.  Without a Parser, such as with clang -E, these callbacks would
never be called.

Fixes PR18576.

Added:
    cfe/trunk/test/Preprocessor/print-pragma-microsoft.c
Modified:
    cfe/trunk/include/clang/Lex/PPCallbacks.h
    cfe/trunk/lib/Frontend/PrintPreprocessedOutput.cpp
    cfe/trunk/lib/Parse/ParsePragma.cpp

Modified: cfe/trunk/include/clang/Lex/PPCallbacks.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/PPCallbacks.h?rev=201821&r1=201820&r2=201821&view=diff
==============================================================================
--- cfe/trunk/include/clang/Lex/PPCallbacks.h (original)
+++ cfe/trunk/include/clang/Lex/PPCallbacks.h Thu Feb 20 16:59:51 2014
@@ -161,18 +161,6 @@ public:
                                PragmaIntroducerKind Introducer) {
   }
 
-  /// \brief Callback invoked when a \#pragma comment directive is read.
-  virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind,
-                             const std::string &Str) {
-  }
-
-  /// \brief Callback invoked when a \#pragma detect_mismatch directive is
-  /// read.
-  virtual void PragmaDetectMismatch(SourceLocation Loc,
-                                    const std::string &Name,
-                                    const std::string &Value) {
-  }
-
   /// \brief Callback invoked when a \#pragma clang __debug directive is read.
   /// \param Loc The location of the debug directive.
   /// \param DebugType The identifier following __debug.
@@ -387,19 +375,6 @@ public:
     Second->Ident(Loc, str);
   }
 
-  virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind,
-                             const std::string &Str) {
-    First->PragmaComment(Loc, Kind, Str);
-    Second->PragmaComment(Loc, Kind, Str);
-  }
-
-  virtual void PragmaDetectMismatch(SourceLocation Loc,
-                                    const std::string &Name,
-                                    const std::string &Value) {
-    First->PragmaDetectMismatch(Loc, Name, Value);
-    Second->PragmaDetectMismatch(Loc, Name, Value);
-  }
-
   virtual void PragmaMessage(SourceLocation Loc, StringRef Namespace,
                              PragmaMessageKind Kind, StringRef Str) {
     First->PragmaMessage(Loc, Namespace, Kind, Str);

Modified: cfe/trunk/lib/Frontend/PrintPreprocessedOutput.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/PrintPreprocessedOutput.cpp?rev=201821&r1=201820&r2=201821&view=diff
==============================================================================
--- cfe/trunk/lib/Frontend/PrintPreprocessedOutput.cpp (original)
+++ cfe/trunk/lib/Frontend/PrintPreprocessedOutput.cpp Thu Feb 20 16:59:51 2014
@@ -138,11 +138,6 @@ public:
                                   const Module *Imported);
   virtual void Ident(SourceLocation Loc, const std::string &str);
   virtual void PragmaCaptured(SourceLocation Loc, StringRef Str);
-  virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind,
-                             const std::string &Str);
-  virtual void PragmaDetectMismatch(SourceLocation Loc,
-                                    const std::string &Name,
-                                    const std::string &Value);
   virtual void PragmaMessage(SourceLocation Loc, StringRef Namespace,
                              PragmaMessageKind Kind, StringRef Str);
   virtual void PragmaDebug(SourceLocation Loc, StringRef DebugType);
@@ -402,36 +397,6 @@ static void outputPrintable(llvm::raw_os
     }
 }
 
-void PrintPPOutputPPCallbacks::PragmaComment(SourceLocation Loc,
-                                             const IdentifierInfo *Kind,
-                                             const std::string &Str) {
-  startNewLineIfNeeded();
-  MoveToLine(Loc);
-  OS << "#pragma comment(" << Kind->getName();
-
-  if (!Str.empty()) {
-    OS << ", \"";
-    outputPrintable(OS, Str);
-    OS << '"';
-  }
-
-  OS << ')';
-  setEmittedDirectiveOnThisLine();
-}
-
-void PrintPPOutputPPCallbacks::PragmaDetectMismatch(SourceLocation Loc,
-                                                    const std::string &Name,
-                                                    const std::string &Value) {
-  startNewLineIfNeeded();
-  MoveToLine(Loc);
-  OS << "#pragma detect_mismatch(\"" << Name << '"';
-  outputPrintable(OS, Name);
-  OS << "\", \"";
-  outputPrintable(OS, Value);
-  OS << "\")";
-  setEmittedDirectiveOnThisLine();
-}
-
 void PrintPPOutputPPCallbacks::PragmaMessage(SourceLocation Loc,
                                              StringRef Namespace,
                                              PragmaMessageKind Kind,
@@ -615,7 +580,13 @@ struct UnknownPragmaHandler : public Pra
         Callbacks->OS << ' ';
       std::string TokSpell = PP.getSpelling(PragmaTok);
       Callbacks->OS.write(&TokSpell[0], TokSpell.size());
-      PP.LexUnexpandedToken(PragmaTok);
+
+      // 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)
+        PP.Lex(PragmaTok);
+      else
+        PP.LexUnexpandedToken(PragmaTok);
     }
     Callbacks->setEmittedDirectiveOnThisLine();
   }

Modified: cfe/trunk/lib/Parse/ParsePragma.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Parse/ParsePragma.cpp?rev=201821&r1=201820&r2=201821&view=diff
==============================================================================
--- cfe/trunk/lib/Parse/ParsePragma.cpp (original)
+++ cfe/trunk/lib/Parse/ParsePragma.cpp Thu Feb 20 16:59:51 2014
@@ -1253,11 +1253,6 @@ void PragmaDetectMismatchHandler::Handle
     return;
   }
 
-  // If the pragma is lexically sound, notify any interested PPCallbacks.
-  if (PP.getPPCallbacks())
-    PP.getPPCallbacks()->PragmaDetectMismatch(CommentLoc, NameString,
-                                              ValueString);
-
   Actions.ActOnPragmaDetectMismatch(NameString, ValueString);
 }
 
@@ -1328,9 +1323,5 @@ void PragmaCommentHandler::HandlePragma(
     return;
   }
 
-  // If the pragma is lexically sound, notify any interested PPCallbacks.
-  if (PP.getPPCallbacks())
-    PP.getPPCallbacks()->PragmaComment(CommentLoc, II, ArgumentString);
-
   Actions.ActOnPragmaMSComment(Kind, ArgumentString);
 }

Added: cfe/trunk/test/Preprocessor/print-pragma-microsoft.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Preprocessor/print-pragma-microsoft.c?rev=201821&view=auto
==============================================================================
--- cfe/trunk/test/Preprocessor/print-pragma-microsoft.c (added)
+++ cfe/trunk/test/Preprocessor/print-pragma-microsoft.c Thu Feb 20 16:59:51 2014
@@ -0,0 +1,20 @@
+// RUN: %clang_cc1 %s -fsyntax-only -fms-extensions -E -o - | FileCheck %s
+
+#define BAR "2"
+#pragma comment(linker, "bar=" BAR)
+// CHECK: #pragma comment(linker, "bar=" "2")
+#pragma comment(user, "Compiled on " __DATE__ " at " __TIME__)
+// CHECK: #pragma comment(user, "Compiled on " "{{[^"]*}}" " at " "{{[^"]*}}")
+
+#define KEY1 "KEY1"
+#define KEY2 "KEY2"
+#define VAL1 "VAL1\""
+#define VAL2 "VAL2"
+
+#pragma detect_mismatch(KEY1 KEY2, VAL1 VAL2)
+// CHECK: #pragma detect_mismatch("KEY1" "KEY2", "VAL1\"" "VAL2")
+
+#define _CRT_PACKING 8
+#pragma pack(push, _CRT_PACKING)
+// CHECK: #pragma pack(push, 8)
+#pragma pack(pop)





More information about the cfe-commits mailing list