r276653 - Support '#pragma once' in headers when using PCH

Sunil Srivastava via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 25 10:17:06 PDT 2016


Author: ssrivastava
Date: Mon Jul 25 12:17:06 2016
New Revision: 276653

URL: http://llvm.org/viewvc/llvm-project?rev=276653&view=rev
Log:
Support '#pragma once' in headers when using PCH
    
The '#pragma once' directive was erroneously ignored when encountered
in the header-file specified in generate-PCH-mode. This resulted in
compile-time errors in some cases with legal code, and also a misleading
warning being produced.

Patch by Warren Ristow!

Differential Revision: http://reviews.llvm.org/D19815

Added:
    cfe/trunk/test/PCH/Inputs/pragma-once.h
    cfe/trunk/test/PCH/pragma-once.c
Modified:
    cfe/trunk/lib/Lex/Pragma.cpp

Modified: cfe/trunk/lib/Lex/Pragma.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Lex/Pragma.cpp?rev=276653&r1=276652&r2=276653&view=diff
==============================================================================
--- cfe/trunk/lib/Lex/Pragma.cpp (original)
+++ cfe/trunk/lib/Lex/Pragma.cpp Mon Jul 25 12:17:06 2016
@@ -352,7 +352,9 @@ void Preprocessor::HandleMicrosoft__prag
 /// HandlePragmaOnce - Handle \#pragma once.  OnceTok is the 'once'.
 ///
 void Preprocessor::HandlePragmaOnce(Token &OnceTok) {
-  if (isInPrimaryFile()) {
+  // Don't honor the 'once' when handling the primary source file, unless
+  // this is a prefix to a TU, which indicates we're generating a PCH file.
+  if (isInPrimaryFile() && TUKind != TU_Prefix) {
     Diag(OnceTok, diag::pp_pragma_once_in_main_file);
     return;
   }

Added: cfe/trunk/test/PCH/Inputs/pragma-once.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/PCH/Inputs/pragma-once.h?rev=276653&view=auto
==============================================================================
--- cfe/trunk/test/PCH/Inputs/pragma-once.h (added)
+++ cfe/trunk/test/PCH/Inputs/pragma-once.h Mon Jul 25 12:17:06 2016
@@ -0,0 +1,5 @@
+#pragma once
+
+/* For use with the pragma-once.c test */
+
+int x = 3;

Added: cfe/trunk/test/PCH/pragma-once.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/PCH/pragma-once.c?rev=276653&view=auto
==============================================================================
--- cfe/trunk/test/PCH/pragma-once.c (added)
+++ cfe/trunk/test/PCH/pragma-once.c Mon Jul 25 12:17:06 2016
@@ -0,0 +1,13 @@
+// Test this without pch.
+// RUN: %clang_cc1 -include %S/Inputs/pragma-once.h -fsyntax-only -verify %s
+
+// Test with pch.
+// RUN: %clang_cc1 -emit-pch -o %t %S/Inputs/pragma-once.h
+// RUN: %clang_cc1 -include-pch %t -fsyntax-only -verify %s
+
+// expected-no-diagnostics
+
+// Including "pragma-once.h" twice, to verify the 'once' aspect is honored.
+#include "Inputs/pragma-once.h"
+#include "Inputs/pragma-once.h"
+int foo(void) { return 0; }




More information about the cfe-commits mailing list