33 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8///9/// \file10/// This file implements a function that runs Clang format on a single11/// 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(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);27 28 // Output must be checked, as otherwise we crash.29 if (!Result) {30 }31 return 0;32}33