diff options
-rw-r--r-- | clang/include/clang/Lex/Preprocessor.h | 3 | ||||
-rw-r--r-- | clang/lib/Frontend/FrontendActions.cpp | 2 | ||||
-rw-r--r-- | clang/lib/Frontend/PrintPreprocessedOutput.cpp | 2 | ||||
-rw-r--r-- | clang/lib/Lex/Pragma.cpp | 22 | ||||
-rw-r--r-- | clang/lib/Rewrite/Frontend/InclusionRewriter.cpp | 8 | ||||
-rw-r--r-- | clang/test/Preprocessor/ignore-pragmas.c | 10 |
6 files changed, 38 insertions, 9 deletions
diff --git a/clang/include/clang/Lex/Preprocessor.h b/clang/include/clang/Lex/Preprocessor.h index 83af8fb3ca6..23445e87a99 100644 --- a/clang/include/clang/Lex/Preprocessor.h +++ b/clang/include/clang/Lex/Preprocessor.h @@ -667,6 +667,9 @@ public: RemovePragmaHandler(StringRef(), Handler); } + /// Install empty handlers for all pragmas (making them ignored). + void IgnorePragmas(); + /// \brief Add the specified comment handler to the preprocessor. void addCommentHandler(CommentHandler *Handler); diff --git a/clang/lib/Frontend/FrontendActions.cpp b/clang/lib/Frontend/FrontendActions.cpp index c8b8a21c508..4cafa237ff2 100644 --- a/clang/lib/Frontend/FrontendActions.cpp +++ b/clang/lib/Frontend/FrontendActions.cpp @@ -592,7 +592,7 @@ void PreprocessOnlyAction::ExecuteAction() { Preprocessor &PP = getCompilerInstance().getPreprocessor(); // Ignore unknown pragmas. - PP.AddPragmaHandler(new EmptyPragmaHandler()); + PP.IgnorePragmas(); Token Tok; // Start parsing the specified input file. diff --git a/clang/lib/Frontend/PrintPreprocessedOutput.cpp b/clang/lib/Frontend/PrintPreprocessedOutput.cpp index 349401ae4ea..94b327534fe 100644 --- a/clang/lib/Frontend/PrintPreprocessedOutput.cpp +++ b/clang/lib/Frontend/PrintPreprocessedOutput.cpp @@ -671,7 +671,7 @@ static int MacroIDCompare(const id_macro_pair *LHS, const id_macro_pair *RHS) { static void DoPrintMacros(Preprocessor &PP, raw_ostream *OS) { // Ignore unknown pragmas. - PP.AddPragmaHandler(new EmptyPragmaHandler()); + PP.IgnorePragmas(); // -dM mode just scans and ignores all tokens in the files, then dumps out // the macro table at the end. diff --git a/clang/lib/Lex/Pragma.cpp b/clang/lib/Lex/Pragma.cpp index 99ba8dee2db..97a08da8ef3 100644 --- a/clang/lib/Lex/Pragma.cpp +++ b/clang/lib/Lex/Pragma.cpp @@ -1385,3 +1385,25 @@ void Preprocessor::RegisterBuiltinPragmas() { AddPragmaHandler(new PragmaRegionHandler("endregion")); } } + +/// Ignore all pragmas, useful for modes such as -Eonly which would otherwise +/// warn about those pragmas being unknown. +void Preprocessor::IgnorePragmas() { + AddPragmaHandler(new EmptyPragmaHandler()); + // Also ignore all pragmas in all namespaces created + // in Preprocessor::RegisterBuiltinPragmas(). + AddPragmaHandler("GCC", new EmptyPragmaHandler()); + AddPragmaHandler("clang", new EmptyPragmaHandler()); + if (PragmaHandler *NS = PragmaHandlers->FindHandler("STDC")) { + // Preprocessor::RegisterBuiltinPragmas() already registers + // PragmaSTDC_UnknownHandler as the empty handler, so remove it first, + // otherwise there will be an assert about a duplicate handler. + PragmaNamespace *STDCNamespace = NS->getIfNamespace(); + assert(STDCNamespace && + "Invalid namespace, registered as a regular pragma handler!"); + if (PragmaHandler *Existing = STDCNamespace->FindHandler("", false)) { + RemovePragmaHandler("STDC", Existing); + } + } + AddPragmaHandler("STDC", new EmptyPragmaHandler()); +} diff --git a/clang/lib/Rewrite/Frontend/InclusionRewriter.cpp b/clang/lib/Rewrite/Frontend/InclusionRewriter.cpp index 70b299213f3..f333191d669 100644 --- a/clang/lib/Rewrite/Frontend/InclusionRewriter.cpp +++ b/clang/lib/Rewrite/Frontend/InclusionRewriter.cpp @@ -515,13 +515,7 @@ void clang::RewriteIncludesInInput(Preprocessor &PP, raw_ostream *OS, InclusionRewriter *Rewrite = new InclusionRewriter(PP, *OS, Opts.ShowLineMarkers); PP.addPPCallbacks(Rewrite); - // Ignore all pragmas, otherwise there will be warnings about unknown pragmas - // (because there's nothing to handle them). - PP.AddPragmaHandler(new EmptyPragmaHandler()); - // Ignore also all pragma in all namespaces created - // in Preprocessor::RegisterBuiltinPragmas(). - PP.AddPragmaHandler("GCC", new EmptyPragmaHandler()); - PP.AddPragmaHandler("clang", new EmptyPragmaHandler()); + PP.IgnorePragmas(); // First let the preprocessor process the entire file and call callbacks. // Callbacks will record which #include's were actually performed. diff --git a/clang/test/Preprocessor/ignore-pragmas.c b/clang/test/Preprocessor/ignore-pragmas.c new file mode 100644 index 00000000000..e2f9ef3dfa3 --- /dev/null +++ b/clang/test/Preprocessor/ignore-pragmas.c @@ -0,0 +1,10 @@ +// RUN: %clang_cc1 -E %s -Wall -verify +// RUN: %clang_cc1 -Eonly %s -Wall -verify +// RUN: %clang -M -Wall %s -Xclang -verify +// RUN: %clang -E -frewrite-includes %s -Wall -Xclang -verify +// RUN: %clang -E -dD -dM %s -Wall -Xclang -verify +// expected-no-diagnostics + +#pragma GCC visibility push (default) +#pragma weak +#pragma this_pragma_does_not_exist |