/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/AST/FormatStringParsing.h
Line | Count | Source |
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/Type.h" |
19 | | #include "clang/AST/FormatString.h" |
20 | | |
21 | | namespace clang { |
22 | | |
23 | | class LangOptions; |
24 | | |
25 | | template <typename T> |
26 | | class UpdateOnReturn { |
27 | | T &ValueToUpdate; |
28 | | const T &ValueToCopy; |
29 | | public: |
30 | | UpdateOnReturn(T &valueToUpdate, const T &valueToCopy) |
31 | 108k | : ValueToUpdate(valueToUpdate), ValueToCopy(valueToCopy) {} |
32 | | |
33 | 108k | ~UpdateOnReturn() { |
34 | 108k | ValueToUpdate = ValueToCopy; |
35 | 108k | } |
36 | | }; |
37 | | |
38 | | namespace analyze_format_string { |
39 | | |
40 | | OptionalAmount ParseAmount(const char *&Beg, const char *E); |
41 | | OptionalAmount ParseNonPositionAmount(const char *&Beg, const char *E, |
42 | | unsigned &argIndex); |
43 | | |
44 | | OptionalAmount ParsePositionAmount(FormatStringHandler &H, |
45 | | const char *Start, const char *&Beg, |
46 | | const char *E, PositionContext p); |
47 | | |
48 | | bool ParseFieldWidth(FormatStringHandler &H, |
49 | | FormatSpecifier &CS, |
50 | | const char *Start, const char *&Beg, const char *E, |
51 | | unsigned *argIndex); |
52 | | |
53 | | bool ParseArgPosition(FormatStringHandler &H, |
54 | | FormatSpecifier &CS, const char *Start, |
55 | | const char *&Beg, const char *E); |
56 | | |
57 | | bool ParseVectorModifier(FormatStringHandler &H, |
58 | | FormatSpecifier &FS, const char *&Beg, const char *E, |
59 | | const LangOptions &LO); |
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, const char *E, |
64 | | const LangOptions &LO, bool IsScanf = false); |
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); |
71 | | |
72 | | template <typename T> class SpecifierResult { |
73 | | T FS; |
74 | | const char *Start; |
75 | | bool Stop; |
76 | | public: |
77 | | SpecifierResult(bool stop = false) |
78 | 14.9k | : Start(nullptr), Stop(stop) {} clang::analyze_format_string::SpecifierResult<clang::analyze_printf::PrintfSpecifier>::SpecifierResult(bool) Line | Count | Source | 78 | 14.7k | : Start(nullptr), Stop(stop) {} |
clang::analyze_format_string::SpecifierResult<clang::analyze_scanf::ScanfSpecifier>::SpecifierResult(bool) Line | Count | Source | 78 | 144 | : Start(nullptr), Stop(stop) {} |
|
79 | | SpecifierResult(const char *start, |
80 | | const T &fs) |
81 | 30.8k | : FS(fs), Start(start), Stop(false) {} clang::analyze_format_string::SpecifierResult<clang::analyze_printf::PrintfSpecifier>::SpecifierResult(char const*, clang::analyze_printf::PrintfSpecifier const&) Line | Count | Source | 81 | 28.4k | : FS(fs), Start(start), Stop(false) {} |
clang::analyze_format_string::SpecifierResult<clang::analyze_scanf::ScanfSpecifier>::SpecifierResult(char const*, clang::analyze_scanf::ScanfSpecifier const&) Line | Count | Source | 81 | 2.38k | : FS(fs), Start(start), Stop(false) {} |
|
82 | | |
83 | 59.0k | const char *getStart() const { return Start; } clang::analyze_format_string::SpecifierResult<clang::analyze_printf::PrintfSpecifier>::getStart() const Line | Count | Source | 83 | 54.2k | const char *getStart() const { return Start; } |
clang::analyze_format_string::SpecifierResult<clang::analyze_scanf::ScanfSpecifier>::getStart() const Line | Count | Source | 83 | 4.77k | const char *getStart() const { return Start; } |
|
84 | 45.7k | bool shouldStop() const { return Stop; } clang::analyze_format_string::SpecifierResult<clang::analyze_printf::PrintfSpecifier>::shouldStop() const Line | Count | Source | 84 | 43.2k | bool shouldStop() const { return Stop; } |
clang::analyze_format_string::SpecifierResult<clang::analyze_scanf::ScanfSpecifier>::shouldStop() const Line | Count | Source | 84 | 2.53k | bool shouldStop() const { return Stop; } |
|
85 | 76.4k | bool hasValue() const { return Start != nullptr; } clang::analyze_format_string::SpecifierResult<clang::analyze_printf::PrintfSpecifier>::hasValue() const Line | Count | Source | 85 | 71.5k | bool hasValue() const { return Start != nullptr; } |
clang::analyze_format_string::SpecifierResult<clang::analyze_scanf::ScanfSpecifier>::hasValue() const Line | Count | Source | 85 | 4.90k | bool hasValue() const { return Start != nullptr; } |
|
86 | 30.8k | const T &getValue() const { |
87 | 30.8k | assert(hasValue()); |
88 | 30.8k | return FS; |
89 | 30.8k | } clang::analyze_format_string::SpecifierResult<clang::analyze_printf::PrintfSpecifier>::getValue() const Line | Count | Source | 86 | 28.4k | const T &getValue() const { | 87 | 28.4k | assert(hasValue()); | 88 | 28.4k | return FS; | 89 | 28.4k | } |
clang::analyze_format_string::SpecifierResult<clang::analyze_scanf::ScanfSpecifier>::getValue() const Line | Count | Source | 86 | 2.38k | const T &getValue() const { | 87 | 2.38k | assert(hasValue()); | 88 | 2.38k | return FS; | 89 | 2.38k | } |
|
90 | | const T &getValue() { return FS; } |
91 | | }; |
92 | | |
93 | | } // end analyze_format_string namespace |
94 | | } // end clang namespace |
95 | | |
96 | | #endif |