1//===-- ClangFormatFuzzer.cpp - Fuzz the Clang format tool ----------------===//
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 Clang format on a single
11/// input. This function is then linked into the Fuzzer library.
12///
13//===----------------------------------------------------------------------===//
14
15#include "clang/Format/Format.h"
16
17extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) {
18 // FIXME: fuzz more things: different styles, different style features.
19 std::string s((const char *)data, size);
20 auto Style = getGoogleStyle(Language: clang::format::FormatStyle::LK_Cpp);
21 Style.ColumnLimit = 60;
22 Style.Macros.push_back(x: "ASSIGN_OR_RETURN(a, b)=a = (b)");
23 Style.Macros.push_back(x: "ASSIGN_OR_RETURN(a, b, c)=a = (b); if (x) return c");
24 Style.Macros.push_back(x: "MOCK_METHOD(r, n, a, s)=r n a s");
25 auto Replaces = reformat(Style, Code: s, Ranges: clang::tooling::Range(0, s.size()));
26 auto Result = applyAllReplacements(Code: s, Replaces);
27
28 // Output must be checked, as otherwise we crash.
29 if (!Result) {
30 }
31 return 0;
32}
33

source code of clang/tools/clang-format/fuzzer/ClangFormatFuzzer.cpp