Index: clang/include/clang/Lex/Preprocessor.h =================================================================== --- clang/include/clang/Lex/Preprocessor.h +++ clang/include/clang/Lex/Preprocessor.h @@ -34,6 +34,7 @@ #include "clang/Lex/TokenLexer.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/DenseMap.h" +#include "llvm/ADT/DenseSet.h" #include "llvm/ADT/FoldingSet.h" #include "llvm/ADT/None.h" #include "llvm/ADT/Optional.h" @@ -325,6 +326,12 @@ /// This is used when loading a precompiled preamble. std::pair SkipMainFilePreamble; + /// Whether we detected a presence of the include cycle. Allows to bail out of + /// preprocessing earlier. + bool HasIncludeCycle = false; + + llvm::DenseSet FilesCausingIncludeCycle; + public: struct PreambleSkipInfo { SourceLocation HashTokenLoc; Index: clang/lib/Lex/PPDirectives.cpp =================================================================== --- clang/lib/Lex/PPDirectives.cpp +++ clang/lib/Lex/PPDirectives.cpp @@ -35,6 +35,7 @@ #include "clang/Lex/Token.h" #include "clang/Lex/VariadicMacroSupport.h" #include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/DenseSet.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/STLExtras.h" @@ -1794,6 +1795,30 @@ // Check that we don't have infinite #include recursion. if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) { Diag(FilenameTok, diag::err_pp_include_too_deep); + + // If the same file is present in the include stack multiple times, treat + // it as a cycle. It is not always correct because the same file can use + // different macro contexts. But together with the fact that we reached + // the max allowed include depth, it is a sufficiently good heuristic to + // detect a cycle. + llvm::DenseSet Includers; + for (auto It = IncludeMacroStack.rbegin(), End = IncludeMacroStack.rend(); It != End; ++It) { + if (IsFileLexer(*It)) + if (const FileEntry *FileEnt = It->ThePPLexer->getFileEntry()) + if (!Includers.insert(FileEnt).second) { + // Found a cycle. Add files comprising a cycle to FilesCausingIncludeCycle. + auto WalkBackIt = It; + const FileEntry *CycleStart = FileEnt; + const FileEntry *File = FileEnt; + do { + if (File) + FilesCausingIncludeCycle.insert(File); + --WalkBackIt; + File = IsFileLexer(*WalkBackIt) ? WalkBackIt->ThePPLexer->getFileEntry() : nullptr; + } while (File != CycleStart); + break; + } + } return; } @@ -1942,10 +1967,10 @@ 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()) + // If we've detected a presence of include cycle, avoid further preprocessing + // as we can end up reaching the max allowed include depth multiple times + // which slows down compilation without providing a value. + if (ShouldEnter && File && llvm::is_contained(FilesCausingIncludeCycle, File)) ShouldEnter = false; // Determine whether we should try to import the module for this #include, if Index: clang/test/Index/Inputs/cycle.h =================================================================== --- /dev/null +++ clang/test/Index/Inputs/cycle.h @@ -0,0 +1 @@ +#include "cycle.h" Index: clang/test/Index/keep-going-include-cycle.c =================================================================== --- /dev/null +++ clang/test/Index/keep-going-include-cycle.c @@ -0,0 +1,9 @@ +#include "cycle.h" +#include "foo.h" + +// RUN: env CINDEXTEST_KEEP_GOING=1 c-index-test -test-print-type -I%S/Inputs %s 2> %t.stderr.txt | FileCheck %s +// RUN: FileCheck -check-prefix CHECK-DIAG %s < %t.stderr.txt + +// CHECK: VarDecl=global_var:1:12 [type=int] [typekind=Int] [isPOD=1] + +// CHECK-DIAG: cycle.h:1:10: error: #include nested too deeply Index: clang/test/Index/keep-going.cpp =================================================================== --- clang/test/Index/keep-going.cpp +++ clang/test/Index/keep-going.cpp @@ -9,11 +9,18 @@ class C : public A { }; -// RUN: env CINDEXTEST_EDITING=1 CINDEXTEST_KEEP_GOING=1 c-index-test -test-print-type %s -std=c++03 2> %t.stderr.txt | FileCheck %s +// Not found includes shouldn't affect subsequent correct includes. +#include "foo.h" + +// RUN: env CINDEXTEST_EDITING=1 CINDEXTEST_KEEP_GOING=1 c-index-test -test-print-type -I%S/Inputs %s -std=c++03 2> %t.stderr.txt | FileCheck %s // RUN: FileCheck -check-prefix CHECK-DIAG %s < %t.stderr.txt +// Verify that even without CINDEXTEST_EDITING we don't stop processing after a fatal error. +// RUN: env CINDEXTEST_KEEP_GOING=1 c-index-test -test-print-type -I%S/Inputs %s -std=c++03 2> %t.stderr.txt | FileCheck -check-prefix CHECK-KEEP-GOING-ONLY %s + // CHECK: inclusion directive=missing1.h ((null)) [type=] [typekind=Invalid] [isPOD=0] // CHECK: inclusion directive=missing2.h ((null)) [type=] [typekind=Invalid] [isPOD=0] +// CHECK: inclusion directive=foo.h ({{.*[/\\]}}test{{[/\\]}}Index{{[/\\]}}Inputs{{[/\\]}}foo.h) [type=] [typekind=Invalid] [isPOD=0] // CHECK: ClassTemplate=A:4:7 (Definition) [type=] [typekind=Invalid] [isPOD=0] // CHECK: TemplateTypeParameter=T:3:16 (Definition) [type=T] [typekind=Unexposed] [canonicaltype=type-parameter-0-0] [canonicaltypekind=Unexposed] [isPOD=0] // CHECK: FieldDecl=a:4:13 (Definition) [type=T] [typekind=Unexposed] [canonicaltype=type-parameter-0-0] [canonicaltypekind=Unexposed] [isPOD=0] @@ -25,5 +32,7 @@ // CHECK: C++ base class specifier=A:4:7 [access=public isVirtual=false] [type=A] [typekind=Unexposed] [templateargs/1= [type=float] [typekind=Float]] [canonicaltype=A] [canonicaltypekind=Record] [canonicaltemplateargs/1= [type=float] [typekind=Float]] [isPOD=0] [nbFields=1] // CHECK: TemplateRef=A:4:7 [type=] [typekind=Invalid] [isPOD=0] +// CHECK-KEEP-GOING-ONLY: VarDecl=global_var:1:12 [type=int] [typekind=Int] [isPOD=1] + // CHECK-DIAG: keep-going.cpp:1:10: fatal error: 'missing1.h' file not found // CHECK-DIAG: keep-going.cpp:8:10: fatal error: 'missing2.h' file not found