From b17dedc4e2f3828d043ebce3032d820d454ab5d8 Mon Sep 17 00:00:00 2001 From: Sunil Srivastava Date: Mon, 25 Jul 2016 17:17:06 +0000 Subject: [PATCH] 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 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@276653 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Lex/Pragma.cpp | 4 +++- test/PCH/Inputs/pragma-once.h | 5 +++++ test/PCH/pragma-once.c | 13 +++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 test/PCH/Inputs/pragma-once.h create mode 100644 test/PCH/pragma-once.c diff --git a/lib/Lex/Pragma.cpp b/lib/Lex/Pragma.cpp index d6e651727c..1ce4b4391f 100644 --- a/lib/Lex/Pragma.cpp +++ b/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/test/PCH/Inputs/pragma-once.h b/test/PCH/Inputs/pragma-once.h new file mode 100644 index 0000000000..831cf553c7 --- /dev/null +++ b/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/test/PCH/pragma-once.c b/test/PCH/pragma-once.c new file mode 100644 index 0000000000..15e8503c11 --- /dev/null +++ b/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; } -- 2.40.0