1 //===-- ClangdFuzzer.cpp - Fuzz clangd ------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// This file implements a function that runs clangd on a single input.
11 /// This function is then linked into the Fuzzer library.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #include "ClangdLSPServer.h"
16 #include "ClangdServer.h"
17 #include "support/ThreadsafeFS.h"
18 #include <cstdio>
19 #include <sstream>
20 
21 using namespace clang::clangd;
22 
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)23 extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) {
24   if (size == 0)
25     return 0;
26 
27   // fmemopen isn't portable, but I think we only run the fuzzer on Linux.
28   std::FILE *In = fmemopen(data, size, "r");
29   auto Transport = newJSONTransport(In, llvm::nulls(),
30                                     /*InMirror=*/nullptr, /*Pretty=*/false,
31                                     /*Style=*/JSONStreamStyle::Delimited);
32   RealThreadsafeFS FS;
33   CodeCompleteOptions CCOpts;
34   ClangdLSPServer::Options Opts;
35   Opts.CodeComplete.EnableSnippets = false;
36   Opts.UseDirBasedCDB = false;
37 
38   // Initialize and run ClangdLSPServer.
39   ClangdLSPServer LSPServer(*Transport, FS, Opts);
40   LSPServer.run();
41   return 0;
42 }
43