From: Lubos Lunak Date: Thu, 1 May 2014 12:54:03 +0000 (+0000) Subject: do not warn about unknown pragmas in modes that do not handle them (pr9537) X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=bce3e035079562c887a4e2e51e4d7e3a370290eb;p=clang do not warn about unknown pragmas in modes that do not handle them (pr9537) And refactor to have just one place in code that sets up the empty pragma handlers. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@207758 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Lex/Preprocessor.h b/include/clang/Lex/Preprocessor.h index 83af8fb3ca..23445e87a9 100644 --- a/include/clang/Lex/Preprocessor.h +++ b/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/lib/Frontend/FrontendActions.cpp b/lib/Frontend/FrontendActions.cpp index c8b8a21c50..4cafa237ff 100644 --- a/lib/Frontend/FrontendActions.cpp +++ b/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/lib/Frontend/PrintPreprocessedOutput.cpp b/lib/Frontend/PrintPreprocessedOutput.cpp index 349401ae4e..94b327534f 100644 --- a/lib/Frontend/PrintPreprocessedOutput.cpp +++ b/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/lib/Lex/Pragma.cpp b/lib/Lex/Pragma.cpp index 99ba8dee2d..97a08da8ef 100644 --- a/lib/Lex/Pragma.cpp +++ b/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/lib/Rewrite/Frontend/InclusionRewriter.cpp b/lib/Rewrite/Frontend/InclusionRewriter.cpp index 70b299213f..f333191d66 100644 --- a/lib/Rewrite/Frontend/InclusionRewriter.cpp +++ b/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/test/Preprocessor/ignore-pragmas.c b/test/Preprocessor/ignore-pragmas.c new file mode 100644 index 0000000000..e2f9ef3dfa --- /dev/null +++ b/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