xref: /minix3/external/bsd/llvm/dist/clang/lib/Analysis/FormatStringParsing.h (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc #ifndef LLVM_CLANG_LIB_ANALYSIS_FORMATSTRINGPARSING_H
2*0a6a1f1dSLionel Sambuc #define LLVM_CLANG_LIB_ANALYSIS_FORMATSTRINGPARSING_H
3f4a2713aSLionel Sambuc 
4f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h"
5f4a2713aSLionel Sambuc #include "clang/AST/Type.h"
6f4a2713aSLionel Sambuc #include "clang/Analysis/Analyses/FormatString.h"
7f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
8f4a2713aSLionel Sambuc 
9f4a2713aSLionel Sambuc namespace clang {
10f4a2713aSLionel Sambuc 
11f4a2713aSLionel Sambuc class LangOptions;
12f4a2713aSLionel Sambuc 
13f4a2713aSLionel Sambuc template <typename T>
14f4a2713aSLionel Sambuc class UpdateOnReturn {
15f4a2713aSLionel Sambuc   T &ValueToUpdate;
16f4a2713aSLionel Sambuc   const T &ValueToCopy;
17f4a2713aSLionel Sambuc public:
UpdateOnReturn(T & valueToUpdate,const T & valueToCopy)18f4a2713aSLionel Sambuc   UpdateOnReturn(T &valueToUpdate, const T &valueToCopy)
19f4a2713aSLionel Sambuc     : ValueToUpdate(valueToUpdate), ValueToCopy(valueToCopy) {}
20f4a2713aSLionel Sambuc 
~UpdateOnReturn()21f4a2713aSLionel Sambuc   ~UpdateOnReturn() {
22f4a2713aSLionel Sambuc     ValueToUpdate = ValueToCopy;
23f4a2713aSLionel Sambuc   }
24f4a2713aSLionel Sambuc };
25f4a2713aSLionel Sambuc 
26f4a2713aSLionel Sambuc namespace analyze_format_string {
27f4a2713aSLionel Sambuc 
28f4a2713aSLionel Sambuc OptionalAmount ParseAmount(const char *&Beg, const char *E);
29f4a2713aSLionel Sambuc OptionalAmount ParseNonPositionAmount(const char *&Beg, const char *E,
30f4a2713aSLionel Sambuc                                       unsigned &argIndex);
31f4a2713aSLionel Sambuc 
32f4a2713aSLionel Sambuc OptionalAmount ParsePositionAmount(FormatStringHandler &H,
33f4a2713aSLionel Sambuc                                    const char *Start, const char *&Beg,
34f4a2713aSLionel Sambuc                                    const char *E, PositionContext p);
35f4a2713aSLionel Sambuc 
36f4a2713aSLionel Sambuc bool ParseFieldWidth(FormatStringHandler &H,
37f4a2713aSLionel Sambuc                      FormatSpecifier &CS,
38f4a2713aSLionel Sambuc                      const char *Start, const char *&Beg, const char *E,
39f4a2713aSLionel Sambuc                      unsigned *argIndex);
40f4a2713aSLionel Sambuc 
41f4a2713aSLionel Sambuc bool ParseArgPosition(FormatStringHandler &H,
42f4a2713aSLionel Sambuc                       FormatSpecifier &CS, const char *Start,
43f4a2713aSLionel Sambuc                       const char *&Beg, const char *E);
44f4a2713aSLionel Sambuc 
45f4a2713aSLionel Sambuc /// Returns true if a LengthModifier was parsed and installed in the
46f4a2713aSLionel Sambuc /// FormatSpecifier& argument, and false otherwise.
47f4a2713aSLionel Sambuc bool ParseLengthModifier(FormatSpecifier &FS, const char *&Beg, const char *E,
48f4a2713aSLionel Sambuc                          const LangOptions &LO, bool IsScanf = false);
49f4a2713aSLionel Sambuc 
50f4a2713aSLionel Sambuc template <typename T> class SpecifierResult {
51f4a2713aSLionel Sambuc   T FS;
52f4a2713aSLionel Sambuc   const char *Start;
53f4a2713aSLionel Sambuc   bool Stop;
54f4a2713aSLionel Sambuc public:
55f4a2713aSLionel Sambuc   SpecifierResult(bool stop = false)
Start(nullptr)56*0a6a1f1dSLionel Sambuc   : Start(nullptr), Stop(stop) {}
SpecifierResult(const char * start,const T & fs)57f4a2713aSLionel Sambuc   SpecifierResult(const char *start,
58f4a2713aSLionel Sambuc                   const T &fs)
59f4a2713aSLionel Sambuc   : FS(fs), Start(start), Stop(false) {}
60f4a2713aSLionel Sambuc 
getStart()61f4a2713aSLionel Sambuc   const char *getStart() const { return Start; }
shouldStop()62f4a2713aSLionel Sambuc   bool shouldStop() const { return Stop; }
hasValue()63*0a6a1f1dSLionel Sambuc   bool hasValue() const { return Start != nullptr; }
getValue()64f4a2713aSLionel Sambuc   const T &getValue() const {
65f4a2713aSLionel Sambuc     assert(hasValue());
66f4a2713aSLionel Sambuc     return FS;
67f4a2713aSLionel Sambuc   }
getValue()68f4a2713aSLionel Sambuc   const T &getValue() { return FS; }
69f4a2713aSLionel Sambuc };
70f4a2713aSLionel Sambuc 
71f4a2713aSLionel Sambuc } // end analyze_format_string namespace
72f4a2713aSLionel Sambuc } // end clang namespace
73f4a2713aSLionel Sambuc 
74f4a2713aSLionel Sambuc #endif
75