[PATCH] D67117: [clangd] Split Preamble.h out of ClangdUnit.h. NFC
This revision was automatically updated to reflect the committed changes. Closed by commit rL370843: [clangd] Split Preamble.h out of ClangdUnit.h. NFC (authored by sammccall, committed by ). Herald added a project: LLVM. Herald added a subscriber: llvm-commits. Changed prior to commit: https://reviews.llvm.org/D67117?vs=218493=218603#toc Repository: rL LLVM CHANGES SINCE LAST ACTION https://reviews.llvm.org/D67117/new/ https://reviews.llvm.org/D67117 Files: clang-tools-extra/trunk/clangd/CMakeLists.txt clang-tools-extra/trunk/clangd/ClangdServer.cpp clang-tools-extra/trunk/clangd/ClangdUnit.cpp clang-tools-extra/trunk/clangd/ClangdUnit.h clang-tools-extra/trunk/clangd/CodeComplete.cpp clang-tools-extra/trunk/clangd/CodeComplete.h clang-tools-extra/trunk/clangd/Compiler.h clang-tools-extra/trunk/clangd/Preamble.cpp clang-tools-extra/trunk/clangd/Preamble.h clang-tools-extra/trunk/clangd/TUScheduler.cpp clang-tools-extra/trunk/clangd/TUScheduler.h clang-tools-extra/trunk/clangd/unittests/TUSchedulerTests.cpp Index: clang-tools-extra/trunk/clangd/TUScheduler.h === --- clang-tools-extra/trunk/clangd/TUScheduler.h +++ clang-tools-extra/trunk/clangd/TUScheduler.h @@ -9,7 +9,7 @@ #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_TUSCHEDULER_H #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_TUSCHEDULER_H -#include "ClangdUnit.h" +#include "Compiler.h" #include "Diagnostics.h" #include "Function.h" #include "GlobalCompilationDatabase.h" @@ -24,6 +24,8 @@ namespace clang { namespace clangd { +class ParsedAST; +struct PreambleData; /// Returns a number of a default async threads to use for TUScheduler. /// Returned value is always >= 1 (i.e. will not cause requests to be processed Index: clang-tools-extra/trunk/clangd/Preamble.h === --- clang-tools-extra/trunk/clangd/Preamble.h +++ clang-tools-extra/trunk/clangd/Preamble.h @@ -0,0 +1,88 @@ +//===--- Preamble.h - Reusing expensive parts of the AST -*- C++-*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// +// +// The vast majority of code in a typical translation unit is in the headers +// included at the top of the file. +// +// The preamble optimization says that we can parse this code once, and reuse +// the result multiple times. The preamble is invalidated by changes to the +// code in the preamble region, to the compile command, or to files on disk. +// +// This is the most important optimization in clangd: it allows operations like +// code-completion to have sub-second latency. It is supported by the +// PrecompiledPreamble functionality in clang, which wraps the techniques used +// by PCH files, modules etc into a convenient interface. +// +//===--===// +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_PREAMBLE_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_PREAMBLE_H + +#include "Compiler.h" +#include "Diagnostics.h" +#include "FS.h" +#include "Headers.h" +#include "index/CanonicalIncludes.h" +#include "clang/Frontend/PrecompiledPreamble.h" +#include "clang/Tooling/CompilationDatabase.h" + +#include +#include +#include + +namespace clang { +namespace clangd { + +/// The parsed preamble and associated data. +/// +/// As we must avoid re-parsing the preamble, any information that can only +/// be obtained during parsing must be eagerly captured and stored here. +struct PreambleData { + PreambleData(PrecompiledPreamble Preamble, std::vector Diags, + IncludeStructure Includes, + std::vector MainFileMacros, + std::unique_ptr StatCache, + CanonicalIncludes CanonIncludes); + + tooling::CompileCommand CompileCommand; + PrecompiledPreamble Preamble; + std::vector Diags; + // Processes like code completions and go-to-definitions will need #include + // information, and their compile action skips preamble range. + IncludeStructure Includes; + // Macros defined in the preamble section of the main file. + // Users care about headers vs main-file, not preamble vs non-preamble. + // These should be treated as main-file entities e.g. for code completion. + std::vector MainFileMacros; + // Cache of FS operations performed when building the preamble. + // When reusing a preamble, this cache can be consumed to save IO. + std::unique_ptr StatCache; + CanonicalIncludes CanonIncludes; +}; + +using PreambleParsedCallback = +std::function, + const CanonicalIncludes &)>; + +/// Build a preamble for the new inputs unless an old one can be reused. +/// If \p OldPreamble can be reused, it is returned unchanged. +/// If \p
[PATCH] D67117: [clangd] Split Preamble.h out of ClangdUnit.h. NFC
ilya-biryukov accepted this revision. ilya-biryukov added a comment. This revision is now accepted and ready to land. LGTM Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D67117/new/ https://reviews.llvm.org/D67117 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[PATCH] D67117: [clangd] Split Preamble.h out of ClangdUnit.h. NFC
sammccall created this revision. Herald added subscribers: cfe-commits, kadircet, arphaman, mgrang, jkorous, MaskRay, javed.absar, ilya-biryukov, mgorny. Herald added a project: clang. Add comment describing use of preamble in clangd. Remove deps on ClangdUnit.h where possible. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D67117 Files: clang-tools-extra/clangd/CMakeLists.txt clang-tools-extra/clangd/ClangdServer.cpp clang-tools-extra/clangd/ClangdUnit.cpp clang-tools-extra/clangd/ClangdUnit.h clang-tools-extra/clangd/CodeComplete.cpp clang-tools-extra/clangd/CodeComplete.h clang-tools-extra/clangd/Compiler.h clang-tools-extra/clangd/Preamble.cpp clang-tools-extra/clangd/Preamble.h clang-tools-extra/clangd/TUScheduler.cpp clang-tools-extra/clangd/TUScheduler.h clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp Index: clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp === --- clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp +++ clang-tools-extra/clangd/unittests/TUSchedulerTests.cpp @@ -12,6 +12,7 @@ #include "Diagnostics.h" #include "Matchers.h" #include "Path.h" +#include "Preamble.h" #include "TUScheduler.h" #include "TestFS.h" #include "Threading.h" Index: clang-tools-extra/clangd/TUScheduler.h === --- clang-tools-extra/clangd/TUScheduler.h +++ clang-tools-extra/clangd/TUScheduler.h @@ -9,7 +9,7 @@ #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_TUSCHEDULER_H #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_TUSCHEDULER_H -#include "ClangdUnit.h" +#include "Compiler.h" #include "Diagnostics.h" #include "Function.h" #include "GlobalCompilationDatabase.h" @@ -24,6 +24,8 @@ namespace clang { namespace clangd { +class ParsedAST; +struct PreambleData; /// Returns a number of a default async threads to use for TUScheduler. /// Returned value is always >= 1 (i.e. will not cause requests to be processed Index: clang-tools-extra/clangd/TUScheduler.cpp === --- clang-tools-extra/clangd/TUScheduler.cpp +++ clang-tools-extra/clangd/TUScheduler.cpp @@ -43,10 +43,12 @@ #include "TUScheduler.h" #include "Cancellation.h" +#include "ClangdUnit.h" #include "Compiler.h" #include "Diagnostics.h" #include "GlobalCompilationDatabase.h" #include "Logger.h" +#include "Preamble.h" #include "Trace.h" #include "index/CanonicalIncludes.h" #include "clang/Frontend/CompilerInvocation.h" Index: clang-tools-extra/clangd/Preamble.h === --- /dev/null +++ clang-tools-extra/clangd/Preamble.h @@ -0,0 +1,88 @@ +//===--- Preamble.h - Reusing expensive parts of the AST -*- C++-*-===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===--===// +// +// The vast majority of code in a typical translation unit is in the headers +// included at the top of the file. +// +// The preamble optimization says that we can parse this code once, and reuse +// the result multiple times. The preamble is invalidated by changes to the +// code in the preamble region, to the compile command, or to files on disk. +// +// This is the most important optimization in clangd: it allows operations like +// code-completion to have sub-second latency. It is supported by the +// PrecompiledPreamble functionality in clang, which wraps the techniques used +// by PCH files, modules etc into a convenient interface. +// +//===--===// +#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_PREAMBLE_H +#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_PREAMBLE_H + +#include "Compiler.h" +#include "Diagnostics.h" +#include "FS.h" +#include "Headers.h" +#include "index/CanonicalIncludes.h" +#include "clang/Frontend/PrecompiledPreamble.h" +#include "clang/Tooling/CompilationDatabase.h" + +#include +#include +#include + +namespace clang { +namespace clangd { + +/// The parsed preamble and associated data. +/// +/// As we must avoid re-parsing the preamble, any information that can only +/// be obtained during parsing must be eagerly captured and stored here. +struct PreambleData { + PreambleData(PrecompiledPreamble Preamble, std::vector Diags, + IncludeStructure Includes, + std::vector MainFileMacros, + std::unique_ptr StatCache, + CanonicalIncludes CanonIncludes); + + tooling::CompileCommand CompileCommand; + PrecompiledPreamble Preamble; + std::vector Diags; + // Processes like code completions and go-to-definitions will need #include + // information, and their compile action skips preamble range. +