1 //===----- FormatStringParsing.h - Format String Parsing --------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This provides some shared functions between printf and scanf format string
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_LIB_ANALYSIS_FORMATSTRINGPARSING_H
15 #define LLVM_CLANG_LIB_ANALYSIS_FORMATSTRINGPARSING_H
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/Type.h"
19 #include "clang/AST/FormatString.h"
26 class UpdateOnReturn
{
30 UpdateOnReturn(T
&valueToUpdate
, const T
&valueToCopy
)
31 : ValueToUpdate(valueToUpdate
), ValueToCopy(valueToCopy
) {}
34 ValueToUpdate
= ValueToCopy
;
38 namespace analyze_format_string
{
40 OptionalAmount
ParseAmount(const char *&Beg
, const char *E
);
41 OptionalAmount
ParseNonPositionAmount(const char *&Beg
, const char *E
,
44 OptionalAmount
ParsePositionAmount(FormatStringHandler
&H
,
45 const char *Start
, const char *&Beg
,
46 const char *E
, PositionContext p
);
48 bool ParseFieldWidth(FormatStringHandler
&H
,
50 const char *Start
, const char *&Beg
, const char *E
,
53 bool ParseArgPosition(FormatStringHandler
&H
,
54 FormatSpecifier
&CS
, const char *Start
,
55 const char *&Beg
, const char *E
);
57 bool ParseVectorModifier(FormatStringHandler
&H
,
58 FormatSpecifier
&FS
, const char *&Beg
, const char *E
,
59 const LangOptions
&LO
);
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
, const char *E
,
64 const LangOptions
&LO
, bool IsScanf
= false);
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
);
72 template <typename T
> class SpecifierResult
{
77 SpecifierResult(bool stop
= false)
78 : Start(nullptr), Stop(stop
) {}
79 SpecifierResult(const char *start
,
81 : FS(fs
), Start(start
), Stop(false) {}
83 const char *getStart() const { return Start
; }
84 bool shouldStop() const { return Stop
; }
85 bool hasValue() const { return Start
!= nullptr; }
86 const T
&getValue() const {
90 const T
&getValue() { return FS
; }
93 } // end analyze_format_string namespace
94 } // end clang namespace