1 //===-- ClangFormatFuzzer.cpp - Fuzz the Clang format tool ----------------===//
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
7 //===----------------------------------------------------------------------===//
10 /// This file implements a function that runs Clang format on a single
11 /// input. This function is then linked into the Fuzzer library.
13 //===----------------------------------------------------------------------===//
15 #include "clang/Format/Format.h"
17 extern "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(clang::format::FormatStyle::LK_Cpp
);
21 Style
.ColumnLimit
= 60;
22 Style
.Macros
.push_back("ASSIGN_OR_RETURN(a, b)=a = (b)");
23 Style
.Macros
.push_back("ASSIGN_OR_RETURN(a, b, c)=a = (b); if (x) return c");
24 Style
.Macros
.push_back("MOCK_METHOD(r, n, a, s)=r n a s");
25 auto Replaces
= reformat(Style
, s
, clang::tooling::Range(0, s
.size()));
26 auto Result
= applyAllReplacements(s
, Replaces
);
28 // Output must be checked, as otherwise we crash.