1//===----- FormatStringParsing.h - Format String Parsing --------*- C++ -*-===//
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-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This provides some shared functions between printf and scanf format string
10// parsing code.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_LIB_ANALYSIS_FORMATSTRINGPARSING_H
15#define LLVM_CLANG_LIB_ANALYSIS_FORMATSTRINGPARSING_H
16
17#include "clang/AST/ASTContext.h"
18#include "clang/AST/FormatString.h"
19#include "clang/AST/Type.h"
20
21namespace clang {
22
23class LangOptions;
24
25template <typename T> class UpdateOnReturn {
26 T &ValueToUpdate;
27 const T &ValueToCopy;
28
29public:
30 UpdateOnReturn(T &valueToUpdate, const T &valueToCopy)
31 : ValueToUpdate(valueToUpdate), ValueToCopy(valueToCopy) {}
32
33 ~UpdateOnReturn() { ValueToUpdate = ValueToCopy; }
34};
35
36namespace analyze_format_string {
37
38class FormatStringParser {
39protected:
40 FormatStringParser(FormatStringHandler &H, const LangOptions &LO,
41 const TargetInfo &Target, bool IsScanf)
42 : H(H), LO(LO), Target(Target), IsScanf(IsScanf) {}
43
44 OptionalAmount parseAmount(const char *&Beg, const char *E) const;
45
46 OptionalAmount parseNonPositionAmount(const char *&Beg, const char *E,
47 unsigned &argIndex) const;
48
49 OptionalAmount parsePositionAmount(const char *Start, const char *&Beg,
50 const char *E, PositionContext p) const;
51
52 bool parseFieldWidth(FormatSpecifier &CS, const char *Start, const char *&Beg,
53 const char *E, unsigned *argIndex) const;
54
55 bool parseArgPosition(FormatSpecifier &CS, const char *Start,
56 const char *&Beg, const char *E) const;
57
58 bool parseVectorModifier(FormatSpecifier &FS, const char *&Beg,
59 const char *E) const;
60
61 /// Returns true if a LengthModifier was parsed and installed in the
62 /// FormatSpecifier& argument, and false otherwise.
63 bool parseLengthModifier(FormatSpecifier &FS, const char *&Beg,
64 const char *E) const;
65
66 /// Returns true if the invalid specifier in \p SpecifierBegin is a UTF-8
67 /// string; check that it won't go further than \p FmtStrEnd and write
68 /// up the total size in \p Len.
69 bool parseUTF8InvalidSpecifier(const char *SpecifierBegin,
70 const char *FmtStrEnd, unsigned &Len) const;
71
72 FormatStringHandler &H;
73 const LangOptions &LO;
74 const TargetInfo &Target;
75 bool IsScanf;
76
77private:
78 bool parseWidthModifier(const char *&I, const char *E, unsigned &BitWidth,
79 unsigned &ModifierLength) const;
80};
81
82template <typename T> class SpecifierResult {
83 T FS;
84 const char *Start;
85 bool Stop;
86
87public:
88 SpecifierResult(bool stop = false) : Start(nullptr), Stop(stop) {}
89 SpecifierResult(const char *start, const T &fs)
90 : FS(fs), Start(start), Stop(false) {}
91
92 const char *getStart() const { return Start; }
93 bool shouldStop() const { return Stop; }
94 bool hasValue() const { return Start != nullptr; }
95 const T &getValue() const {
96 assert(hasValue());
97 return FS;
98 }
99 const T &getValue() { return FS; }
100};
101
102} // namespace analyze_format_string
103} // namespace clang
104
105#endif
106