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

Warren Ristow via cfe-commits cfe-commits at lists.llvm.org
Mon May 16 00:39:14 PDT 2016


wristow updated this revision to Diff 57320.
wristow added a comment.

Simplified he implementation, by using the existing `TUKind` field.


http://reviews.llvm.org/D19815

Files:
  llvm/tools/clang/lib/Lex/Pragma.cpp
  llvm/tools/clang/test/PCH/Inputs/pragma-once.h
  llvm/tools/clang/test/PCH/pragma-once.c

Index: llvm/tools/clang/test/PCH/pragma-once.c
===================================================================
--- /dev/null
+++ llvm/tools/clang/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: llvm/tools/clang/test/PCH/Inputs/pragma-once.h
===================================================================
--- /dev/null
+++ llvm/tools/clang/test/PCH/Inputs/pragma-once.h
@@ -0,0 +1,5 @@
+#pragma once
+
+/* For use with the pragma-once.c test */
+
+int x = 3;
Index: llvm/tools/clang/lib/Lex/Pragma.cpp
===================================================================
--- llvm/tools/clang/lib/Lex/Pragma.cpp
+++ llvm/tools/clang/lib/Lex/Pragma.cpp
@@ -354,7 +354,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.57320.patch
Type: text/x-patch
Size: 1562 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160516/dff2ed57/attachment.bin>


More information about the cfe-commits mailing list