1 //===-- ClangFormatFuzzer.cpp - Fuzz the Clang format tool ----------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 /// 10 /// \file 11 /// This file implements a function that runs Clang format on a single 12 /// input. This function is then linked into the Fuzzer library. 13 /// 14 //===----------------------------------------------------------------------===// 15 16 #include "clang/Format/Format.h" 17 18 extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) { 19 // FIXME: fuzz more things: different styles, different style features. 20 std::string s((const char *)data, size); 21 auto Style = getGoogleStyle(clang::format::FormatStyle::LK_Cpp); 22 Style.ColumnLimit = 60; 23 auto Replaces = reformat(Style, s, clang::tooling::Range(0, s.size())); 24 auto Result = applyAllReplacements(s, Replaces); 25 26 // Output must be checked, as otherwise we crash. 27 if (!Result) {} 28 return 0; 29 } 30