//= FormatString.h - Analysis of printf/fprintf format strings --*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This file defines APIs for analyzing the format strings of printf, fscanf, // and friends. // // The structure of format strings for fprintf are described in C99 7.19.6.1. // // The structure of format strings for fscanf are described in C99 7.19.6.2. // //===----------------------------------------------------------------------===// #ifndef LLVM_CLANG_AST_FORMATSTRING_H #define LLVM_CLANG_AST_FORMATSTRING_H #include "clang/AST/CanonicalType.h" #include <optional> namespace clang { class TargetInfo; //===----------------------------------------------------------------------===// /// Common components of both fprintf and fscanf format strings. namespace analyze_format_string { /// Class representing optional flags with location and representation /// information. class OptionalFlag { … }; /// Represents the length modifier in a format string in scanf/printf. class LengthModifier { … }; class ConversionSpecifier { … }; class ArgType { … }; class OptionalAmount { … }; class FormatSpecifier { … }; } // end analyze_format_string namespace //===----------------------------------------------------------------------===// /// Pieces specific to fprintf format strings. namespace analyze_printf { class PrintfConversionSpecifier : public analyze_format_string::ConversionSpecifier { … }; ArgType; LengthModifier; OptionalAmount; OptionalFlag; class PrintfSpecifier : public analyze_format_string::FormatSpecifier { … }; } // end analyze_printf namespace //===----------------------------------------------------------------------===// /// Pieces specific to fscanf format strings. namespace analyze_scanf { class ScanfConversionSpecifier : public analyze_format_string::ConversionSpecifier { … }; ArgType; LengthModifier; OptionalAmount; OptionalFlag; class ScanfSpecifier : public analyze_format_string::FormatSpecifier { … }; } // end analyze_scanf namespace //===----------------------------------------------------------------------===// // Parsing and processing of format strings (both fprintf and fscanf). namespace analyze_format_string { enum PositionContext { … }; class FormatStringHandler { … }; bool ParsePrintfString(FormatStringHandler &H, const char *beg, const char *end, const LangOptions &LO, const TargetInfo &Target, bool isFreeBSDKPrintf); bool ParseFormatStringHasSArg(const char *beg, const char *end, const LangOptions &LO, const TargetInfo &Target); bool ParseScanfString(FormatStringHandler &H, const char *beg, const char *end, const LangOptions &LO, const TargetInfo &Target); /// Return true if the given string has at least one formatting specifier. bool parseFormatStringHasFormattingSpecifiers(const char *Begin, const char *End, const LangOptions &LO, const TargetInfo &Target); } // end analyze_format_string namespace } // end clang namespace #endif