summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--clang/lib/Lex/Pragma.cpp4
-rw-r--r--clang/test/PCH/Inputs/pragma-once.h5
-rw-r--r--clang/test/PCH/pragma-once.c13
3 files changed, 21 insertions, 1 deletions
diff --git a/clang/lib/Lex/Pragma.cpp b/clang/lib/Lex/Pragma.cpp
index d6e651727cc..1ce4b4391f6 100644
--- a/clang/lib/Lex/Pragma.cpp
+++ b/clang/lib/Lex/Pragma.cpp
@@ -352,7 +352,9 @@ void Preprocessor::HandleMicrosoft__pragma(Token &Tok) {
/// 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;
}
diff --git a/clang/test/PCH/Inputs/pragma-once.h b/clang/test/PCH/Inputs/pragma-once.h
new file mode 100644
index 00000000000..831cf553c76
--- /dev/null
+++ b/clang/test/PCH/Inputs/pragma-once.h
@@ -0,0 +1,5 @@
+#pragma once
+
+/* For use with the pragma-once.c test */
+
+int x = 3;
diff --git a/clang/test/PCH/pragma-once.c b/clang/test/PCH/pragma-once.c
new file mode 100644
index 00000000000..15e8503c115
--- /dev/null
+++ b/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; }
OpenPOWER on IntegriCloud