File: main.cc

package info (click to toggle)
ccls 0.20210330-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,340 kB
  • sloc: cpp: 13,398; python: 152; makefile: 15; objc: 15; ansic: 1
file content (151 lines) | stat: -rw-r--r-- 4,628 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2017-2018 ccls Authors
// SPDX-License-Identifier: Apache-2.0

#include "log.hh"
#include "pipeline.hh"
#include "platform.hh"
#include "serializer.hh"
#include "test.hh"
#include "working_files.hh"

#include <clang/Basic/Version.h>
#include <llvm/Support/CommandLine.h>
#include <llvm/Support/CrashRecoveryContext.h>
#include <llvm/Support/FileSystem.h>
#include <llvm/Support/Process.h>
#include <llvm/Support/Program.h>
#include <llvm/Support/Signals.h>

#include <rapidjson/document.h>
#include <rapidjson/error/en.h>

#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <unordered_map>
#include <vector>

using namespace ccls;
using namespace llvm;
using namespace llvm::cl;

namespace ccls {
std::vector<std::string> g_init_options;
}

namespace {
OptionCategory C("ccls options");

opt<bool> opt_help("h", desc("Alias for -help"), cat(C));
opt<int> opt_verbose("v", desc("verbosity, from -3 (fatal) to 2 (verbose)"),
                     init(0), cat(C));
opt<std::string> opt_test_index("test-index", ValueOptional, init("!"),
                                desc("run index tests"), cat(C));

opt<std::string> opt_index("index",
                           desc("standalone mode: index a project and exit"),
                           value_desc("root"), cat(C));
list<std::string> opt_init("init", desc("extra initialization options in JSON"),
                           cat(C));
opt<std::string> opt_log_file("log-file", desc("stderr or log file"),
                              value_desc("file"), init("stderr"), cat(C));
opt<bool> opt_log_file_append("log-file-append", desc("append to log file"),
                              cat(C));

void closeLog() { fclose(ccls::log::file); }

} // namespace

int main(int argc, char **argv) {
  traceMe();
  sys::PrintStackTraceOnErrorSignal(argv[0]);
  cl::SetVersionPrinter([](raw_ostream &os) {
    os << clang::getClangToolFullVersion("ccls version " CCLS_VERSION "\nclang")
       << "\n";
  });

  cl::HideUnrelatedOptions(C);

  ParseCommandLineOptions(argc, argv,
                          "C/C++/Objective-C language server\n\n"
                          "See more on https://github.com/MaskRay/ccls/wiki");

  if (opt_help) {
    PrintHelpMessage();
    return 0;
  }
  ccls::log::verbosity = ccls::log::Verbosity(opt_verbose.getValue());

  pipeline::init();
  const char *env = getenv("CCLS_CRASH_RECOVERY");
  if (!env || strcmp(env, "0") != 0)
    CrashRecoveryContext::Enable();

  bool language_server = true;

  if (opt_log_file.size()) {
    ccls::log::file =
        opt_log_file == "stderr"
            ? stderr
            : fopen(opt_log_file.c_str(), opt_log_file_append ? "ab" : "wb");
    if (!ccls::log::file) {
      fprintf(stderr, "failed to open %s\n", opt_log_file.c_str());
      return 2;
    }
    setbuf(ccls::log::file, NULL);
    atexit(closeLog);
  }

  if (opt_test_index != "!") {
    language_server = false;
    if (!ccls::runIndexTests(opt_test_index,
                             sys::Process::StandardInIsUserInput()))
      return 1;
  }

  if (language_server) {
    if (!opt_init.empty()) {
      // We check syntax error here but override client-side
      // initializationOptions in messages/initialize.cc
      g_init_options = opt_init;
      rapidjson::Document reader;
      for (const std::string &str : g_init_options) {
        rapidjson::ParseResult ok = reader.Parse(str.c_str());
        if (!ok) {
          fprintf(stderr, "Failed to parse --init as JSON: %s (%zd)\n",
                  rapidjson::GetParseError_En(ok.Code()), ok.Offset());
          return 1;
        }
        JsonReader json_reader{&reader};
        try {
          Config config;
          reflect(json_reader, config);
        } catch (std::invalid_argument &e) {
          fprintf(stderr, "Failed to parse --init %s, expected %s\n",
                  static_cast<JsonReader &>(json_reader).getPath().c_str(),
                  e.what());
          return 1;
        }
      }
    }

    sys::ChangeStdinToBinary();
    sys::ChangeStdoutToBinary();
    if (opt_index.size()) {
      SmallString<256> root(opt_index);
      sys::fs::make_absolute(root);
      pipeline::standalone(std::string(root.data(), root.size()));
    } else {
      // The thread that reads from stdin and dispatchs commands to the main
      // thread.
      pipeline::launchStdin();
      // The thread that writes responses from the main thread to stdout.
      pipeline::launchStdout();
      // Main thread which also spawns indexer threads upon the "initialize"
      // request.
      pipeline::mainLoop();
    }
  }

  return 0;
}