60 lines · c
1//===-- dictionary.c - Generate fuzzing dictionary for clang --------------===//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// This binary emits a fuzzing dictionary describing strings that are10// significant to the clang parser: keywords and other tokens.11//12// The dictionary can be used by a fuzzer to reach interesting parser states13// much more quickly.14//15// The output is a single-file dictionary supported by libFuzzer and AFL:16// https://llvm.org/docs/LibFuzzer.html#dictionaries17//18//===----------------------------------------------------------------------===//19 20#include <stdio.h>21 22static void emit(const char *Name, const char *Spelling) {23 static char Hex[] = "0123456789abcdef";24 // Skip EmptySpellingName for IsDeducible.25 if (!Name[0]) return;26 27 printf("%s=\"", Name);28 unsigned char C;29 while ((C = *Spelling++)) {30 if (C < 32 || C == '"' || C == '\\')31 printf("\\x%c%c", Hex[C>>4], Hex[C%16]);32 else33 printf("%c", C);34 }35 printf("\"\n");36}37 38int main(int argc, char **argv) {39#define PUNCTUATOR(Name, Spelling) emit(#Name, Spelling);40#define KEYWORD(Name, Criteria) emit(#Name, #Name);41#define PPKEYWORD(Name) emit(#Name, #Name);42#define CXX_KEYWORD_OPERATOR(Name, Equivalent) emit(#Name, #Name);43#define OBJC_AT_KEYWORD(Name) emit(#Name, #Name);44#define ALIAS(Spelling, Equivalent, Criteria) emit(Spelling, Spelling);45#include "clang/Basic/TokenKinds.def"46 // Some other sub-token chunks significant to the lexer.47 emit("ucn16", "\\u0000");48 emit("ucn32", "\\U00000000");49 emit("rawstart", "R\"(");50 emit("rawend", ")\"");51 emit("quote", "\"");52 emit("squote", "'");53 emit("u8quote", "u8\"");54 emit("u16quote", "u\"");55 emit("u32quote", "U\"");56 emit("esc_nl", "\\\n");57 emit("hex", "0x");58}59 60