diff --git a/clang-tools-extra/clangd/ClangdServer.h b/clang-tools-extra/clangd/ClangdServer.h index 555f387ff3297e..612524aae98dfc 100644 --- a/clang-tools-extra/clangd/ClangdServer.h +++ b/clang-tools-extra/clangd/ClangdServer.h @@ -128,11 +128,13 @@ class ClangdServer { /// enabled. ClangTidyOptionsBuilder GetClangTidyOptions; - /// If true, turn on the `-frecovery-ast` clang flag. - bool BuildRecoveryAST = true; + /// If true, force -frecovery-ast flag. + /// If false, respect the value in clang. + bool BuildRecoveryAST = false; - /// If true, turn on the `-frecovery-ast-type` clang flag. - bool PreserveRecoveryASTType = true; + /// If true, force -frecovery-ast-type flag. + /// If false, respect the value in clang. + bool PreserveRecoveryASTType = false; /// Clangd's workspace root. Relevant for "workspace" operations not bound /// to a particular file. diff --git a/clang-tools-extra/clangd/Compiler.cpp b/clang-tools-extra/clangd/Compiler.cpp index f5875e2a7971f3..c22585fc53f9da 100644 --- a/clang-tools-extra/clangd/Compiler.cpp +++ b/clang-tools-extra/clangd/Compiler.cpp @@ -81,11 +81,11 @@ buildCompilerInvocation(const ParseInputs &Inputs, clang::DiagnosticConsumer &D, // Don't crash on `#pragma clang __debug parser_crash` CI->getPreprocessorOpts().DisablePragmaDebugCrash = true; - // Recovery expression currently only works for C++. - if (CI->getLangOpts()->CPlusPlus) { - CI->getLangOpts()->RecoveryAST = Inputs.Opts.BuildRecoveryAST; - CI->getLangOpts()->RecoveryASTType = Inputs.Opts.PreserveRecoveryASTType; - } + if (Inputs.Opts.BuildRecoveryAST) + CI->getLangOpts()->RecoveryAST = true; + if (Inputs.Opts.PreserveRecoveryASTType) + CI->getLangOpts()->RecoveryASTType = true; + return CI; } diff --git a/clang-tools-extra/clangd/tool/ClangdMain.cpp b/clang-tools-extra/clangd/tool/ClangdMain.cpp index 78d8355a2c5d93..faa8c7508a93fd 100644 --- a/clang-tools-extra/clangd/tool/ClangdMain.cpp +++ b/clang-tools-extra/clangd/tool/ClangdMain.cpp @@ -310,7 +310,7 @@ opt CrossFileRename{ opt RecoveryAST{ "recovery-ast", cat(Features), - desc("Preserve expressions in AST for broken code (C++ only)."), + desc("Preserve expressions in AST for broken code."), init(ClangdServer::Options().BuildRecoveryAST), }; diff --git a/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp b/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp index de73bc66a17831..510f358920c14e 100644 --- a/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp +++ b/clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp @@ -111,8 +111,6 @@ CodeCompleteResult completions(const TestTU &TU, Position Point, MockFS FS; auto Inputs = TU.inputs(FS); - Inputs.Opts.BuildRecoveryAST = true; - Inputs.Opts.PreserveRecoveryASTType = true; IgnoreDiagnostics Diags; auto CI = buildCompilerInvocation(Inputs, Diags); if (!CI) { @@ -1100,8 +1098,6 @@ SignatureHelp signatures(llvm::StringRef Text, Position Point, MockFS FS; auto Inputs = TU.inputs(FS); Inputs.Index = Index.get(); - Inputs.Opts.BuildRecoveryAST = true; - Inputs.Opts.PreserveRecoveryASTType = true; IgnoreDiagnostics Diags; auto CI = buildCompilerInvocation(Inputs, Diags); if (!CI) { diff --git a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp index 1ea2aa34f289cd..a68ea1d6a3dcbf 100644 --- a/clang-tools-extra/clangd/unittests/FindTargetTests.cpp +++ b/clang-tools-extra/clangd/unittests/FindTargetTests.cpp @@ -148,6 +148,17 @@ TEST_F(TargetDeclTest, Exprs) { EXPECT_DECLS("LabelStmt", "label:"); } +TEST_F(TargetDeclTest, RecoveryForC) { + Flags = {"-xc", "-Xclang", "-frecovery-ast"}; + Code = R"cpp( + // error-ok: testing behavior on broken code + // int f(); + int f(int); + int x = [[f]](); + )cpp"; + EXPECT_DECLS("DeclRefExpr", "int f(int)"); +} + TEST_F(TargetDeclTest, Recovery) { Code = R"cpp( // error-ok: testing behavior on broken code diff --git a/clang-tools-extra/clangd/unittests/TestTU.cpp b/clang-tools-extra/clangd/unittests/TestTU.cpp index 03254f8767213c..81b6b43da94b44 100644 --- a/clang-tools-extra/clangd/unittests/TestTU.cpp +++ b/clang-tools-extra/clangd/unittests/TestTU.cpp @@ -59,8 +59,6 @@ ParseInputs TestTU::inputs(MockFS &FS) const { FS.OverlayRealFileSystemForModules = true; Inputs.TFS = &FS; Inputs.Opts = ParseOptions(); - Inputs.Opts.BuildRecoveryAST = true; - Inputs.Opts.PreserveRecoveryASTType = true; Inputs.Opts.ClangTidyOpts.Checks = ClangTidyChecks; Inputs.Opts.ClangTidyOpts.WarningsAsErrors = ClangTidyWarningsAsErrors; Inputs.Index = ExternalIndex;