xref: /openbsd-src/gnu/llvm/clang/tools/clang-format/fuzzer/ClangFormatFuzzer.cpp (revision e5dd70708596ae51455a0ffa086a00c5b29f8583)
1*e5dd7070Spatrick //===-- ClangFormatFuzzer.cpp - Fuzz the Clang format tool ----------------===//
2*e5dd7070Spatrick //
3*e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5*e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*e5dd7070Spatrick //
7*e5dd7070Spatrick //===----------------------------------------------------------------------===//
8*e5dd7070Spatrick ///
9*e5dd7070Spatrick /// \file
10*e5dd7070Spatrick /// This file implements a function that runs Clang format on a single
11*e5dd7070Spatrick ///  input. This function is then linked into the Fuzzer library.
12*e5dd7070Spatrick ///
13*e5dd7070Spatrick //===----------------------------------------------------------------------===//
14*e5dd7070Spatrick 
15*e5dd7070Spatrick #include "clang/Format/Format.h"
16*e5dd7070Spatrick 
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)17*e5dd7070Spatrick extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) {
18*e5dd7070Spatrick   // FIXME: fuzz more things: different styles, different style features.
19*e5dd7070Spatrick   std::string s((const char *)data, size);
20*e5dd7070Spatrick   auto Style = getGoogleStyle(clang::format::FormatStyle::LK_Cpp);
21*e5dd7070Spatrick   Style.ColumnLimit = 60;
22*e5dd7070Spatrick   auto Replaces = reformat(Style, s, clang::tooling::Range(0, s.size()));
23*e5dd7070Spatrick   auto Result = applyAllReplacements(s, Replaces);
24*e5dd7070Spatrick 
25*e5dd7070Spatrick   // Output must be checked, as otherwise we crash.
26*e5dd7070Spatrick   if (!Result) {}
27*e5dd7070Spatrick   return 0;
28*e5dd7070Spatrick }
29