[PATCH] D19815: Support '#pragma once' in headers when using PCH

Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 25 10:24:44 PDT 2016


This revision was automatically updated to reflect the committed changes.
Closed by commit rL276653: Support '#pragma once' in headers when using PCH (authored by ssrivastava).

Changed prior to commit:
  https://reviews.llvm.org/D19815?vs=57320&id=65372#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D19815

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

Index: cfe/trunk/test/PCH/Inputs/pragma-once.h
===================================================================
--- cfe/trunk/test/PCH/Inputs/pragma-once.h
+++ cfe/trunk/test/PCH/Inputs/pragma-once.h
@@ -0,0 +1,5 @@
+#pragma once
+
+/* For use with the pragma-once.c test */
+
+int x = 3;
Index: cfe/trunk/test/PCH/pragma-once.c
===================================================================
--- cfe/trunk/test/PCH/pragma-once.c
+++ cfe/trunk/test/PCH/pragma-once.c
@@ -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; }
Index: cfe/trunk/lib/Lex/Pragma.cpp
===================================================================
--- cfe/trunk/lib/Lex/Pragma.cpp
+++ cfe/trunk/lib/Lex/Pragma.cpp
@@ -352,7 +352,9 @@
 /// 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;
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D19815.65372.patch
Type: text/x-patch
Size: 1566 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160725/1900c33f/attachment.bin>


More information about the cfe-commits mailing list