diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp index 4ea0f485d31d..d8dae73037a8 100644 --- a/clang/lib/Lex/PPDirectives.cpp +++ b/clang/lib/Lex/PPDirectives.cpp @@ -1896,6 +1896,12 @@ void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc, if (PPOpts->SingleFileParseMode) ShouldEnter = false; + // Any diagnostics after the fatal error will not be visible. As the + // compilation failed already and errors in subsequently included files won't + // be visible, avoid preprocessing those files. + if (ShouldEnter && Diags->hasFatalErrorOccurred()) + ShouldEnter = false; + // Determine whether we should try to import the module for this #include, if // there is one. Don't do so if precompiled module support is disabled or we // are processing this module textually (because we're building the module). diff --git a/clang/test/Preprocessor/Inputs/cycle/a.h b/clang/test/Preprocessor/Inputs/cycle/a.h new file mode 100644 index 000000000000..dd3ef35d61fc --- /dev/null +++ b/clang/test/Preprocessor/Inputs/cycle/a.h @@ -0,0 +1,8 @@ +// Presence of 2 inclusion cycles +// b.h -> a.h -> b.h -> ... +// c.h -> a.h -> c.h -> ... +// makes it unfeasible to reach max inclusion depth in all possible ways. Need +// to stop earlier. + +#include "b.h" +#include "c.h" diff --git a/clang/test/Preprocessor/Inputs/cycle/b.h b/clang/test/Preprocessor/Inputs/cycle/b.h new file mode 100644 index 000000000000..2243de1baf9a --- /dev/null +++ b/clang/test/Preprocessor/Inputs/cycle/b.h @@ -0,0 +1 @@ +#include "a.h" diff --git a/clang/test/Preprocessor/Inputs/cycle/c.h b/clang/test/Preprocessor/Inputs/cycle/c.h new file mode 100644 index 000000000000..2243de1baf9a --- /dev/null +++ b/clang/test/Preprocessor/Inputs/cycle/c.h @@ -0,0 +1 @@ +#include "a.h" diff --git a/clang/test/Preprocessor/include-cycle.c b/clang/test/Preprocessor/include-cycle.c new file mode 100644 index 000000000000..52fcfbd27ad8 --- /dev/null +++ b/clang/test/Preprocessor/include-cycle.c @@ -0,0 +1,5 @@ +// RUN: not %clang_cc1 -E -I%S/Inputs -ferror-limit 20 %s + +// Test that preprocessing terminates even if we have inclusion cycles. + +#include "cycle/a.h"