]> granicus.if.org Git - clang/commitdiff
Parser support for #pragma clang __debug captured
authorTareq A. Siraj <tareq.a.sriaj@intel.com>
Tue, 16 Apr 2013 18:41:26 +0000 (18:41 +0000)
committerTareq A. Siraj <tareq.a.sriaj@intel.com>
Tue, 16 Apr 2013 18:41:26 +0000 (18:41 +0000)
This patch implements parsing ‘#pragma clang __debug’ as a first step for
implementing captured statements. Captured statements are a mechanism for
doing outlining in the AST.
see http://lists.cs.uiuc.edu/pipermail/cfe-dev/2013-January/027540.html.

Currently returns StmtEmpty

Author: Andy Zhang <andy.zhang@intel.com>

Differential Revision: http://llvm-reviews.chandlerc.com/D369

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@179614 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/TokenKinds.def
include/clang/Lex/PPCallbacks.h
include/clang/Parse/Parser.h
lib/Frontend/PrintPreprocessedOutput.cpp
lib/Lex/Pragma.cpp
lib/Parse/ParsePragma.cpp
lib/Parse/ParseStmt.cpp
test/Parser/captured-statements.c [new file with mode: 0644]
test/Preprocessor/pragma-captured.c [new file with mode: 0644]

index fb2edcec0d39e4dda6578ebb90cf41d3cbb21234..bcf0f31dcbf6f4b0830a81cbe3082aafd3b0d50b 100644 (file)
@@ -615,6 +615,11 @@ ANNOTATION(pragma_pack)
 // handles them.
 ANNOTATION(pragma_parser_crash)
 
+// Annotation for #pragma clang __debug captured...
+// The lexer produces these so that they only take effect when the parser
+// handles them.
+ANNOTATION(pragma_captured)
+
 // Annotation for #pragma ms_struct...
 // The lexer produces these so that they only take effect when the parser
 // handles them.
index 96359a2aa3a46771c1731496820b5a35156dd00d..661a3a79ec50604689424532c17d0a3899839b32 100644 (file)
@@ -159,6 +159,12 @@ public:
                              const std::string &Str) {
   }
 
+  /// \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.
+  virtual void PragmaDebug(SourceLocation Loc, StringRef DebugType) {
+  }
+
   /// \brief Callback invoked when a \#pragma message directive is read.
   /// \param Loc The location of the message directive.
   /// \param Str The text of the message directive.
index 8cc60a29dfa3a594f1312b3d161fd9c9d519346c..812a09baa6dad12dff7b2d63162e9b75093d72a9 100644 (file)
@@ -422,6 +422,10 @@ private:
   /// #pragma OPENCL EXTENSION...
   void HandlePragmaOpenCLExtension();
 
+  /// \brief Handle the annotation token produced for
+  /// #pragma clang __debug captured
+  StmtResult HandlePragmaCaptured();
+
   /// GetLookAheadToken - This peeks ahead N tokens and returns that token
   /// without consuming any tokens.  LookAhead(0) returns 'Tok', LookAhead(1)
   /// returns the token after Tok, etc.
index 432428bb96906bd54588d0b56d81381688bacb2f..6734d7759b4da91a08e044e7a8bf4acf441bf0e8 100644 (file)
@@ -140,6 +140,7 @@ public:
   virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind,
                              const std::string &Str);
   virtual void PragmaMessage(SourceLocation Loc, StringRef Str);
+  virtual void PragmaDebug(SourceLocation Loc, StringRef DebugType);
   virtual void PragmaDiagnosticPush(SourceLocation Loc,
                                     StringRef Namespace);
   virtual void PragmaDiagnosticPop(SourceLocation Loc,
@@ -419,6 +420,17 @@ void PrintPPOutputPPCallbacks::PragmaMessage(SourceLocation Loc,
   setEmittedDirectiveOnThisLine();
 }
 
+void PrintPPOutputPPCallbacks::PragmaDebug(SourceLocation Loc,
+                                           StringRef DebugType) {
+  startNewLineIfNeeded();
+  MoveToLine(Loc);
+
+  OS << "#pragma clang __debug ";
+  OS << DebugType;
+
+  setEmittedDirectiveOnThisLine();
+}
+
 void PrintPPOutputPPCallbacks::
 PragmaDiagnosticPush(SourceLocation Loc, StringRef Namespace) {
   startNewLineIfNeeded();
index 95e8a8ca8fc86189046d21fffe61b35f2e6c79be..af58c50f054bfab23ca1e0a6f023a0e037f8b5bd 100644 (file)
@@ -995,10 +995,40 @@ struct PragmaDebugHandler : public PragmaHandler {
       llvm::CrashRecoveryContext *CRC =llvm::CrashRecoveryContext::GetCurrent();
       if (CRC)
         CRC->HandleCrash();
+    } else if (II->isStr("captured")) {
+      HandleCaptured(PP);
     } else {
       PP.Diag(Tok, diag::warn_pragma_debug_unexpected_command)
         << II->getName();
     }
+
+    PPCallbacks *Callbacks = PP.getPPCallbacks();
+    if (Callbacks)
+      Callbacks->PragmaDebug(Tok.getLocation(), II->getName());
+  }
+
+  void HandleCaptured(Preprocessor &PP) {
+    // Skip if emitting preprocessed output.
+    if (PP.isPreprocessedOutput())
+      return;
+
+    Token Tok;
+    PP.LexUnexpandedToken(Tok);
+
+    if (Tok.isNot(tok::eod)) {
+      PP.Diag(Tok, diag::ext_pp_extra_tokens_at_eol)
+        << "pragma clang __debug captured";
+      return;
+    }
+
+    SourceLocation NameLoc = Tok.getLocation();
+    Token *Toks = PP.getPreprocessorAllocator().Allocate<Token>(1);
+    Toks->startToken();
+    Toks->setKind(tok::annot_pragma_captured);
+    Toks->setLocation(NameLoc);
+
+    PP.EnterTokenStream(Toks, 1, /*DisableMacroExpansion=*/true,
+                        /*OwnsTokens=*/false);
   }
 
 // Disable MSVC warning about runtime stack overflow.
index dc6b3ed4fa9816f226d13b4187cf92ce6131c23b..038636d10801ff440af980339cbc9753ac6bd5fb 100644 (file)
@@ -122,6 +122,19 @@ void Parser::HandlePragmaFPContract() {
   ConsumeToken(); // The annotation token.
 }
 
+StmtResult Parser::HandlePragmaCaptured()
+{
+  assert(Tok.is(tok::annot_pragma_captured));
+  ConsumeToken();
+
+  if (Tok.isNot(tok::l_brace)) {
+    PP.Diag(Tok, diag::err_expected_lbrace);
+    return StmtError();
+  }
+
+  return StmtEmpty();
+}
+
 namespace {
   typedef llvm::PointerIntPair<IdentifierInfo *, 1, bool> OpenCLExtData;
 }
@@ -151,6 +164,8 @@ void Parser::HandlePragmaOpenCLExtension() {
   }
 }
 
+
+
 // #pragma GCC visibility comes in two variants:
 //   'push' '(' [visibility] ')'
 //   'pop'
index 355f3694bb6cafd66e7ff0c278932cb14868337e..5fa4f170265f1208f715bbae47500063e3cf518d 100644 (file)
@@ -289,6 +289,9 @@ Retry:
     HandlePragmaOpenCLExtension();
     return StmtEmpty();
 
+  case tok::annot_pragma_captured:
+    return HandlePragmaCaptured();
+
   case tok::annot_pragma_openmp:
     SourceLocation DeclStart = Tok.getLocation();
     DeclGroupPtrTy Res = ParseOpenMPDeclarativeDirective();
diff --git a/test/Parser/captured-statements.c b/test/Parser/captured-statements.c
new file mode 100644 (file)
index 0000000..30dddb5
--- /dev/null
@@ -0,0 +1,14 @@
+// RUN: %clang_cc1 -verify %s
+
+void test1()
+{
+  #pragma clang __debug captured x // expected-warning {{extra tokens at end of #pragma clang __debug captured directive}}
+  {
+  }
+}
+
+void test2()
+{
+  #pragma clang __debug captured
+  int x; // expected-error {{expected '{'}}
+}
diff --git a/test/Preprocessor/pragma-captured.c b/test/Preprocessor/pragma-captured.c
new file mode 100644 (file)
index 0000000..be2a62b
--- /dev/null
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 -E %s | FileCheck %s
+
+// Test pragma clang __debug captured, for Captured Statements
+
+void test1()
+{
+  #pragma clang __debug captured
+  {
+  }
+// CHECK: void test1()
+// CHECK: {
+// CHECK: #pragma clang __debug captured
+}