From: Daniel Dunbar Date: Fri, 11 Jun 2010 20:10:12 +0000 (+0000) Subject: Preprocessor: Ignore unknown pragmas in -E -dM and -Eonly modes. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c72cc5072cdc1a1a6e05f9d0f962f293a69248c4;p=clang Preprocessor: Ignore unknown pragmas in -E -dM and -Eonly modes. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@105830 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Lex/Pragma.h b/include/clang/Lex/Pragma.h index ef367feb84..52f01a9a97 100644 --- a/include/clang/Lex/Pragma.h +++ b/include/clang/Lex/Pragma.h @@ -46,6 +46,15 @@ public: virtual PragmaNamespace *getIfNamespace() { return 0; } }; +/// EmptyPragmaHandler - A pragma handler which takes no action, which can be +/// used to ignore particular pragmas. +class EmptyPragmaHandler : public PragmaHandler { +public: + EmptyPragmaHandler(); + + virtual void HandlePragma(Preprocessor &PP, Token &FirstToken); +}; + /// PragmaNamespace - This PragmaHandler subdivides the namespace of pragmas, /// allowing hierarchical pragmas to be defined. Common examples of namespaces /// are "#pragma GCC", "#pragma STDC", and "#pragma omp", but any namespaces may diff --git a/lib/Frontend/FrontendActions.cpp b/lib/Frontend/FrontendActions.cpp index 6cd960be20..421d69d243 100644 --- a/lib/Frontend/FrontendActions.cpp +++ b/lib/Frontend/FrontendActions.cpp @@ -9,6 +9,7 @@ #include "clang/Frontend/FrontendActions.h" #include "clang/AST/ASTConsumer.h" +#include "clang/Lex/Pragma.h" #include "clang/Lex/Preprocessor.h" #include "clang/Parse/Parser.h" #include "clang/Basic/FileManager.h" @@ -223,6 +224,9 @@ void ParseOnlyAction::ExecuteAction() { void PreprocessOnlyAction::ExecuteAction() { Preprocessor &PP = getCompilerInstance().getPreprocessor(); + // Ignore unknown pragmas. + PP.AddPragmaHandler(0, new EmptyPragmaHandler()); + Token Tok; // Start parsing the specified input file. PP.EnterMainSourceFile(); diff --git a/lib/Frontend/PrintPreprocessedOutput.cpp b/lib/Frontend/PrintPreprocessedOutput.cpp index b6c18b7731..e89c425f47 100644 --- a/lib/Frontend/PrintPreprocessedOutput.cpp +++ b/lib/Frontend/PrintPreprocessedOutput.cpp @@ -454,6 +454,9 @@ static int MacroIDCompare(const void* a, const void* b) { } static void DoPrintMacros(Preprocessor &PP, llvm::raw_ostream *OS) { + // Ignore unknown pragmas. + PP.AddPragmaHandler(0, new EmptyPragmaHandler()); + // -dM mode just scans and ignores all tokens in the files, then dumps out // the macro table at the end. PP.EnterMainSourceFile(); diff --git a/lib/Lex/Pragma.cpp b/lib/Lex/Pragma.cpp index 92332a0068..58a632618f 100644 --- a/lib/Lex/Pragma.cpp +++ b/lib/Lex/Pragma.cpp @@ -26,6 +26,14 @@ using namespace clang; PragmaHandler::~PragmaHandler() { } +//===----------------------------------------------------------------------===// +// EmptyPragmaHandler Implementation. +//===----------------------------------------------------------------------===// + +EmptyPragmaHandler::EmptyPragmaHandler() : PragmaHandler(0) {} + +void EmptyPragmaHandler::HandlePragma(Preprocessor &PP, Token &FirstToken) {} + //===----------------------------------------------------------------------===// // PragmaNamespace Implementation. //===----------------------------------------------------------------------===// diff --git a/test/Frontend/unknown-pragmas.c b/test/Frontend/unknown-pragmas.c new file mode 100644 index 0000000000..53a5a45a43 --- /dev/null +++ b/test/Frontend/unknown-pragmas.c @@ -0,0 +1,4 @@ +// RUN: %clang_cc1 -Eonly -Wall -verify %s +// RUN: %clang_cc1 -E -dM -Wall -verify %s + +#pragma adgohweopihweotnwet