[clang-tools-extra] 5696f2d - [clangd] Move --check options into Check.cpp. Add --check-completion.

Sam McCall via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 21 07:39:34 PST 2022


Author: Sam McCall
Date: 2022-11-21T16:39:27+01:00
New Revision: 5696f2dfce5f23967a047f68897a7ff2e3d8e7d1

URL: https://github.com/llvm/llvm-project/commit/5696f2dfce5f23967a047f68897a7ff2e3d8e7d1
DIFF: https://github.com/llvm/llvm-project/commit/5696f2dfce5f23967a047f68897a7ff2e3d8e7d1.diff

LOG: [clangd] Move --check options into Check.cpp. Add --check-completion.

This is less plumbing and clutter in ClangdMain.cpp.
Having --check-lines imply completion was just about minimizing plumbing
I think, so make that explicit.

Added: 
    

Modified: 
    clang-tools-extra/clangd/tool/Check.cpp
    clang-tools-extra/clangd/tool/ClangdMain.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/tool/Check.cpp b/clang-tools-extra/clangd/tool/Check.cpp
index 40a545e240d4..9517d74123c6 100644
--- a/clang-tools-extra/clangd/tool/Check.cpp
+++ b/clang-tools-extra/clangd/tool/Check.cpp
@@ -60,10 +60,22 @@ namespace clangd {
 namespace {
 
 // These will never be shown in --help, ClangdMain doesn't list the category.
-llvm::cl::opt<std::string> CheckTidyTime(
+llvm::cl::opt<std::string> CheckTidyTime{
     "check-tidy-time",
     llvm::cl::desc("Print the overhead of checks matching this glob"),
-    llvm::cl::init(""));
+    llvm::cl::init("")};
+llvm::cl::opt<std::string> CheckFileLines{
+    "check-lines",
+    llvm::cl::desc(
+        "Limits the range of tokens in -check file on which "
+        "various features are tested. Example --check-lines=3-7 restricts "
+        "testing to lines 3 to 7 (inclusive) or --check-lines=5 to restrict "
+        "to one line. Default is testing entire file."),
+    llvm::cl::init("")};
+llvm::cl::opt<bool> CheckCompletion{
+    "check-completion",
+    llvm::cl::desc("Run code-completion at each point (slow)"),
+    llvm::cl::init(false)};
 
 // Print (and count) the error-level diagnostics (warnings are ignored).
 unsigned showErrors(llvm::ArrayRef<Diag> Diags) {
@@ -330,8 +342,7 @@ class Checker {
   }
 
   // Run AST-based features at each token in the file.
-  void testLocationFeatures(llvm::Optional<Range> LineRange,
-                            const bool EnableCodeCompletion) {
+  void testLocationFeatures(llvm::Optional<Range> LineRange) {
     trace::Span Trace("testLocationFeatures");
     log("Testing features at each token (may be slow in large files)");
     auto &SM = AST->getSourceManager();
@@ -383,7 +394,7 @@ class Checker {
       unsigned DocHighlights = findDocumentHighlights(*AST, Pos).size();
       vlog("    documentHighlight: {0}", DocHighlights);
 
-      if (EnableCodeCompletion) {
+      if (CheckCompletion) {
         Position EndPos = offsetToPosition(Inputs.Contents, End);
         auto CC = codeComplete(File, EndPos, Preamble.get(), Inputs, CCOpts);
         vlog("    code completion: {0}",
@@ -395,9 +406,27 @@ class Checker {
 
 } // namespace
 
-bool check(llvm::StringRef File, llvm::Optional<Range> LineRange,
-           const ThreadsafeFS &TFS, const ClangdLSPServer::Options &Opts,
-           bool EnableCodeCompletion) {
+bool check(llvm::StringRef File, const ThreadsafeFS &TFS,
+           const ClangdLSPServer::Options &Opts) {
+  llvm::Optional<Range> LineRange;
+  if (!CheckFileLines.empty()) {
+    uint32_t Begin = 0, End = std::numeric_limits<uint32_t>::max();
+    StringRef RangeStr(CheckFileLines);
+    bool ParseError = RangeStr.consumeInteger(0, Begin);
+    if (RangeStr.empty()) {
+      End = Begin;
+    } else {
+      ParseError |= !RangeStr.consume_front("-");
+      ParseError |= RangeStr.consumeInteger(0, End);
+    }
+    if (ParseError || !RangeStr.empty() || Begin <= 0 || End < Begin) {
+      elog("Invalid --check-lines specified. Use Begin-End format, e.g. 3-17");
+      return false;
+    }
+    LineRange = Range{Position{static_cast<int>(Begin - 1), 0},
+                      Position{static_cast<int>(End), 0}};
+  }
+
   llvm::SmallString<0> FakeFile;
   llvm::Optional<std::string> Contents;
   if (File.empty()) {
@@ -426,7 +455,7 @@ bool check(llvm::StringRef File, llvm::Optional<Range> LineRange,
     return false;
   C.buildInlayHints(LineRange);
   C.buildSemanticHighlighting(LineRange);
-  C.testLocationFeatures(LineRange, EnableCodeCompletion);
+  C.testLocationFeatures(LineRange);
 
   log("All checks completed, {0} errors", C.ErrCount);
   return C.ErrCount == 0;

diff  --git a/clang-tools-extra/clangd/tool/ClangdMain.cpp b/clang-tools-extra/clangd/tool/ClangdMain.cpp
index 53b6653c51f2..22d407be5033 100644
--- a/clang-tools-extra/clangd/tool/ClangdMain.cpp
+++ b/clang-tools-extra/clangd/tool/ClangdMain.cpp
@@ -61,9 +61,8 @@ namespace clang {
 namespace clangd {
 
 // Implemented in Check.cpp.
-bool check(const llvm::StringRef File, llvm::Optional<Range> LineRange,
-           const ThreadsafeFS &TFS, const ClangdLSPServer::Options &Opts,
-           bool EnableCodeCompletion);
+bool check(const llvm::StringRef File, const ThreadsafeFS &TFS,
+           const ClangdLSPServer::Options &Opts);
 
 namespace {
 
@@ -392,17 +391,6 @@ opt<Path> CheckFile{
     ValueOptional,
 };
 
-opt<std::string> CheckFileLines{
-    "check-lines",
-    cat(Misc),
-    desc("If specified, limits the range of tokens in -check file on which "
-         "various features are tested. Example --check-lines=3-7 restricts "
-         "testing to lines 3 to 7 (inclusive) or --check-lines=5 to restrict "
-         "to one line. Default is testing entire file."),
-    init(""),
-    ValueOptional,
-};
-
 enum PCHStorageFlag { Disk, Memory };
 opt<PCHStorageFlag> PCHStorage{
     "pch-storage",
@@ -985,37 +973,10 @@ clangd accepts flags on the commandline, and in the CLANGD_FLAGS environment var
       return 1;
     }
     log("Entering check mode (no LSP server)");
-    llvm::Optional<Range> CheckLineRange;
-    if (!CheckFileLines.empty()) {
-      uint32_t Begin = 0, End = std::numeric_limits<uint32_t>::max();
-      StringRef RangeStr(CheckFileLines);
-      bool ParseError = RangeStr.consumeInteger(0, Begin);
-      if (RangeStr.empty()) {
-        End = Begin;
-      } else {
-        ParseError |= !RangeStr.consume_front("-");
-        ParseError |= RangeStr.consumeInteger(0, End);
-      }
-      if (ParseError || !RangeStr.empty() || Begin <= 0 || End < Begin) {
-        elog(
-            "Invalid --check-lines specified. Use Begin-End format, e.g. 3-17");
-        return 1;
-      }
-      CheckLineRange = Range{Position{static_cast<int>(Begin - 1), 0},
-                             Position{static_cast<int>(End), 0}};
-    }
-    // For now code completion is enabled any time the range is limited via
-    // --check-lines. If it turns out to be to slow, we can introduce a
-    // dedicated flag for that instead.
-    return check(Path, CheckLineRange, TFS, Opts,
-                 /*EnableCodeCompletion=*/!CheckFileLines.empty())
+    return check(Path, TFS, Opts)
                ? 0
                : static_cast<int>(ErrorResultCode::CheckFailed);
   }
-  if (!CheckFileLines.empty()) {
-    elog("--check-lines requires --check");
-    return 1;
-  }
 
   // Initialize and run ClangdLSPServer.
   // Change stdin to binary to not lose \r\n on windows.


        


More information about the cfe-commits mailing list