diff --git a/clang/include/clang/Basic/DiagnosticCommonKinds.td b/clang/include/clang/Basic/DiagnosticCommonKinds.td --- a/clang/include/clang/Basic/DiagnosticCommonKinds.td +++ b/clang/include/clang/Basic/DiagnosticCommonKinds.td @@ -282,6 +282,10 @@ "file '%0' modified since it was first processed">, DefaultFatal; def err_file_too_large : Error< "sorry, unsupported: file '%0' is too large for Clang to process">; +def err_include_too_large : Error< + "sorry, this include generates a translation unit too large for" + " Clang to process. This may by a result from multiple" + " inclusions of unguarded header files.">, DefaultFatal; def err_unsupported_bom : Error<"%0 byte order mark detected in '%1', but " "encoding is not supported">, DefaultFatal; def err_unable_to_rename_temp : Error< diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp --- a/clang/lib/Basic/SourceManager.cpp +++ b/clang/lib/Basic/SourceManager.cpp @@ -577,13 +577,18 @@ SLocEntryLoaded[Index] = true; return FileID::get(LoadedID); } + unsigned FileSize = File->getSize(); + if (!(NextLocalOffset + FileSize + 1 > NextLocalOffset && + NextLocalOffset + FileSize + 1 <= CurrentLoadedOffset)) { + // From this point, there is no sensible way to point to the current + // source-location and say: This include at line ### generates a too + // big file, as the IncludePos received is + Diag.Report(IncludePos, diag::err_include_too_large); + return FileID(); + } LocalSLocEntryTable.push_back( SLocEntry::get(NextLocalOffset, FileInfo::get(IncludePos, File, FileCharacter, Filename))); - unsigned FileSize = File->getSize(); - assert(NextLocalOffset + FileSize + 1 > NextLocalOffset && - NextLocalOffset + FileSize + 1 <= CurrentLoadedOffset && - "Ran out of source locations!"); // We do a +1 here because we want a SourceLocation that means "the end of the // file", e.g. for the "no newline at the end of the file" diagnostic. NextLocalOffset += FileSize + 1;