diff options
| -rw-r--r-- | clang/include/clang/Basic/DiagnosticLexKinds.td | 2 | ||||
| -rw-r--r-- | clang/lib/Basic/SourceManager.cpp | 2 | ||||
| -rw-r--r-- | clang/lib/Lex/PPDirectives.cpp | 12 |
3 files changed, 15 insertions, 1 deletions
diff --git a/clang/include/clang/Basic/DiagnosticLexKinds.td b/clang/include/clang/Basic/DiagnosticLexKinds.td index 5c567f21320..dd5e2af3d27 100644 --- a/clang/include/clang/Basic/DiagnosticLexKinds.td +++ b/clang/include/clang/Basic/DiagnosticLexKinds.td @@ -426,6 +426,8 @@ def note_pp_framework_without_header : Note< "did not find header '%0' in framework '%1' (loaded from '%2')">; def err_pp_error_opening_file : Error< "error opening file '%0': %1">, DefaultFatal; +def err_pp_including_mainfile_in_preamble : Error< + "main file cannot be included recursively when building a preamble">; def err_pp_empty_filename : Error<"empty filename">; def err_pp_include_too_deep : Error<"#include nested too deeply">; def err_pp_expects_filename : Error<"expected \"FILENAME\" or <FILENAME>">; diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp index 0ffad1bc475..712cbb0d0ea 100644 --- a/clang/lib/Basic/SourceManager.cpp +++ b/clang/lib/Basic/SourceManager.cpp @@ -1582,7 +1582,7 @@ FileID SourceManager::translateFile(const FileEntry *SourceFile) const { if (MainSLoc.isFile()) { const ContentCache *MainContentCache = MainSLoc.getFile().getContentCache(); - if (!MainContentCache) { + if (!MainContentCache || !MainContentCache->OrigEntry) { // Can't do anything } else if (MainContentCache->OrigEntry == SourceFile) { FirstFID = MainFileID; 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 |

