summaryrefslogtreecommitdiffstats
path: root/clang/lib/Lex/PPDirectives.cpp
diff options
context:
space:
mode:
authorNikolai Kosjar <nikolai.kosjar@qt.io>2019-05-10 10:25:35 +0000
committerNikolai Kosjar <nikolai.kosjar@qt.io>2019-05-10 10:25:35 +0000
commit3c28a2dc6bdc331e5a0d8097a5fa59d06682b9d0 (patch)
tree8a3a3ca4f74d123c53d7ec4d27aa926ef22fd5bd /clang/lib/Lex/PPDirectives.cpp
parenta2ab528c4ca8d03846ebf6ce42efd6424a6556f1 (diff)
downloadbcm5719-llvm-3c28a2dc6bdc331e5a0d8097a5fa59d06682b9d0.tar.gz
bcm5719-llvm-3c28a2dc6bdc331e5a0d8097a5fa59d06682b9d0.zip
[Preamble] Stop circular inclusion of main file when building preamble
If a header file was processed for the second time, we could end up with a wrong conditional stack and skipped ranges: In the particular example, if the header guard is evaluated the second time and it is decided to skip the conditional block, the corresponding "#endif" is never seen since the preamble does not include it and we end up in the Tok.is(tok::eof) case with a wrong conditional stack. Detect the circular inclusion, emit a diagnostic and stop processing the inclusion. llvm-svn: 360418
Diffstat (limited to 'clang/lib/Lex/PPDirectives.cpp')
-rw-r--r--clang/lib/Lex/PPDirectives.cpp12
1 files changed, 12 insertions, 0 deletions
diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp
index 5d5cae5fd0f..193388650d2 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -1871,6 +1871,18 @@ Preprocessor::ImportAction Preprocessor::HandleHeaderIncludeOrImport(
return {ImportAction::None};
}
+ // Check for circular inclusion of the main file.
+ // We can't generate a consistent preamble with regard to the conditional
+ // stack if the main file is included again as due to the preamble bounds
+ // some directives (e.g. #endif of a header guard) will never be seen.
+ // Since this will lead to confusing errors, avoid the inclusion.
+ if (File && PreambleConditionalStack.isRecording() &&
+ SourceMgr.translateFile(File) == SourceMgr.getMainFileID()) {
+ Diag(FilenameTok.getLocation(),
+ diag::err_pp_including_mainfile_in_preamble);
+ return {ImportAction::None};
+ }
+
// Should we enter the source file? Set to Skip if either the source file is
// known to have no effect beyond its effect on module visibility -- that is,
// if it's got an include guard that is already defined, set to Import if it
OpenPOWER on IntegriCloud