Benjamin Kramer | 2f5db8b | 2014-08-13 16:25:19 +0000 | [diff] [blame] | 1 | #ifndef LLVM_CLANG_LIB_ANALYSIS_FORMATSTRINGPARSING_H |
| 2 | #define LLVM_CLANG_LIB_ANALYSIS_FORMATSTRINGPARSING_H |
Ted Kremenek | 575e89d | 2010-07-16 02:11:26 +0000 | [diff] [blame] | 3 | |
Ted Kremenek | 575e89d | 2010-07-16 02:11:26 +0000 | [diff] [blame] | 4 | #include "clang/AST/ASTContext.h" |
| 5 | #include "clang/AST/Type.h" |
Tim Northover | 314fbfa | 2018-11-02 13:14:11 +0000 | [diff] [blame] | 6 | #include "clang/AST/FormatString.h" |
Ted Kremenek | 575e89d | 2010-07-16 02:11:26 +0000 | [diff] [blame] | 7 | |
Dan Gohman | 28ade55 | 2010-07-26 21:25:24 +0000 | [diff] [blame] | 8 | namespace clang { |
| 9 | |
Hans Wennborg | 23926bd | 2011-12-15 10:25:47 +0000 | [diff] [blame] | 10 | class LangOptions; |
| 11 | |
Ted Kremenek | 575e89d | 2010-07-16 02:11:26 +0000 | [diff] [blame] | 12 | template <typename T> |
| 13 | class UpdateOnReturn { |
| 14 | T &ValueToUpdate; |
| 15 | const T &ValueToCopy; |
| 16 | public: |
| 17 | UpdateOnReturn(T &valueToUpdate, const T &valueToCopy) |
| 18 | : ValueToUpdate(valueToUpdate), ValueToCopy(valueToCopy) {} |
| 19 | |
| 20 | ~UpdateOnReturn() { |
| 21 | ValueToUpdate = ValueToCopy; |
| 22 | } |
| 23 | }; |
| 24 | |
Ted Kremenek | 575e89d | 2010-07-16 02:11:26 +0000 | [diff] [blame] | 25 | namespace analyze_format_string { |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 26 | |
Ted Kremenek | 575e89d | 2010-07-16 02:11:26 +0000 | [diff] [blame] | 27 | OptionalAmount ParseAmount(const char *&Beg, const char *E); |
| 28 | OptionalAmount ParseNonPositionAmount(const char *&Beg, const char *E, |
| 29 | unsigned &argIndex); |
| 30 | |
| 31 | OptionalAmount ParsePositionAmount(FormatStringHandler &H, |
| 32 | const char *Start, const char *&Beg, |
| 33 | const char *E, PositionContext p); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 34 | |
Ted Kremenek | 575e89d | 2010-07-16 02:11:26 +0000 | [diff] [blame] | 35 | bool ParseFieldWidth(FormatStringHandler &H, |
| 36 | FormatSpecifier &CS, |
| 37 | const char *Start, const char *&Beg, const char *E, |
| 38 | unsigned *argIndex); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 39 | |
Ted Kremenek | 575e89d | 2010-07-16 02:11:26 +0000 | [diff] [blame] | 40 | bool ParseArgPosition(FormatStringHandler &H, |
| 41 | FormatSpecifier &CS, const char *Start, |
| 42 | const char *&Beg, const char *E); |
| 43 | |
Matt Arsenault | 0ff50d4 | 2018-12-01 22:16:27 +0000 | [diff] [blame] | 44 | bool ParseVectorModifier(FormatStringHandler &H, |
| 45 | FormatSpecifier &FS, const char *&Beg, const char *E, |
| 46 | const LangOptions &LO); |
| 47 | |
Ted Kremenek | 575e89d | 2010-07-16 02:11:26 +0000 | [diff] [blame] | 48 | /// Returns true if a LengthModifier was parsed and installed in the |
| 49 | /// FormatSpecifier& argument, and false otherwise. |
Hans Wennborg | 23926bd | 2011-12-15 10:25:47 +0000 | [diff] [blame] | 50 | bool ParseLengthModifier(FormatSpecifier &FS, const char *&Beg, const char *E, |
| 51 | const LangOptions &LO, bool IsScanf = false); |
Bruno Cardoso Lopes | 0c18d03 | 2016-03-29 17:35:02 +0000 | [diff] [blame] | 52 | |
| 53 | /// Returns true if the invalid specifier in \p SpecifierBegin is a UTF-8 |
| 54 | /// string; check that it won't go further than \p FmtStrEnd and write |
| 55 | /// up the total size in \p Len. |
| 56 | bool ParseUTF8InvalidSpecifier(const char *SpecifierBegin, |
| 57 | const char *FmtStrEnd, unsigned &Len); |
| 58 | |
Ted Kremenek | 575e89d | 2010-07-16 02:11:26 +0000 | [diff] [blame] | 59 | template <typename T> class SpecifierResult { |
| 60 | T FS; |
| 61 | const char *Start; |
| 62 | bool Stop; |
| 63 | public: |
| 64 | SpecifierResult(bool stop = false) |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 65 | : Start(nullptr), Stop(stop) {} |
Ted Kremenek | 575e89d | 2010-07-16 02:11:26 +0000 | [diff] [blame] | 66 | SpecifierResult(const char *start, |
| 67 | const T &fs) |
| 68 | : FS(fs), Start(start), Stop(false) {} |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 69 | |
Ted Kremenek | 575e89d | 2010-07-16 02:11:26 +0000 | [diff] [blame] | 70 | const char *getStart() const { return Start; } |
| 71 | bool shouldStop() const { return Stop; } |
Craig Topper | 2554294 | 2014-05-20 04:30:07 +0000 | [diff] [blame] | 72 | bool hasValue() const { return Start != nullptr; } |
Ted Kremenek | 575e89d | 2010-07-16 02:11:26 +0000 | [diff] [blame] | 73 | const T &getValue() const { |
| 74 | assert(hasValue()); |
| 75 | return FS; |
| 76 | } |
| 77 | const T &getValue() { return FS; } |
| 78 | }; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 79 | |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 80 | } // end analyze_format_string namespace |
| 81 | } // end clang namespace |
Ted Kremenek | 575e89d | 2010-07-16 02:11:26 +0000 | [diff] [blame] | 82 | |
| 83 | #endif |