]> granicus.if.org Git - clang/commitdiff
do not warn about unknown pragmas in modes that do not handle them (pr9537)
authorLubos Lunak <l.lunak@centrum.cz>
Thu, 1 May 2014 12:54:03 +0000 (12:54 +0000)
committerLubos Lunak <l.lunak@centrum.cz>
Thu, 1 May 2014 12:54:03 +0000 (12:54 +0000)
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

include/clang/Lex/Preprocessor.h
lib/Frontend/FrontendActions.cpp
lib/Frontend/PrintPreprocessedOutput.cpp
lib/Lex/Pragma.cpp
lib/Rewrite/Frontend/InclusionRewriter.cpp
test/Preprocessor/ignore-pragmas.c [new file with mode: 0644]

index 83af8fb3ca6afc6d4cd82a7070ad80c11226688c..23445e87a99b06a1c5e58abed31ddab7be2f10a1 100644 (file)
@@ -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);
 
index c8b8a21c508f2646db0eca47ffa8b2c35a3c75bd..4cafa237ff2dee32d44b1ec7d65faa37a4aaad0b 100644 (file)
@@ -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.
index 349401ae4ead7e044d75b8563798f1feab72ad5d..94b327534fea8942d07c38faf5c3276ad8f980f4 100644 (file)
@@ -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.
index 99ba8dee2dbab367b88011706915ef8ab6c4efc6..97a08da8ef3e88b6037ce749afd482d641415f09 100644 (file)
@@ -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());
+}
index 70b299213f38c7886bcc9c5328851f3e91a492e2..f333191d66929c04a551378dd43e8c4cd18dd5c1 100644 (file)
@@ -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 (file)
index 0000000..e2f9ef3
--- /dev/null
@@ -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