aboutsummaryrefslogtreecommitdiff
path: root/include/clang/Analysis/Analyses/PrintfFormatString.h
blob: 978486d271aeaa82ce73d3da4ef5920d3e0ed4d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//==- PrintfFormatStrings.h - Analysis of printf format strings --*- C++ -*-==//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Handling of format string in printf and friends.  The structure of format
// strings for fprintf() are described in C99 7.19.6.1.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_FPRINTF_FORMAT_H
#define LLVM_CLANG_FPRINTF_FORMAT_H

#include <cassert>

namespace clang {
namespace printf {

class ConversionSpecifier {
public:
  enum Kind {
   InvalidSpecifier = 0,
   dArg, // 'd'
   iArg, // 'i',
   oArg, // 'o',
   uArg, // 'u',
   xArg, // 'x',
   XArg, // 'X',
   fArg, // 'f',
   FArg, // 'F',
   eArg, // 'e',
   EArg, // 'E',
   gArg, // 'g',
   GArg, // 'G',
   aArg, // 'a',
   AArg, // 'A',
   IntAsCharArg,  // 'c'
   CStrArg,       // 's'
   VoidPtrArg,    // 'p'
   OutIntPtrArg,  // 'n'
   PercentArg,    // '%'
   IntArgBeg = dArg,
   IntArgEnd = iArg,
   UIntArgBeg = oArg,
   UIntArgEnd = XArg,
   DoubleArgBeg = fArg,
   DoubleArgEnd = AArg
  };

  ConversionSpecifier(Kind k) : kind(k) {}
  
  bool isIntArg() const { return kind >= dArg && kind <= iArg; }
  bool isUIntArg() const { return kind >= oArg && kind <= XArg; }
  bool isDoubleArg() const { return kind >= fArg && kind <= AArg; }
  Kind getKind() const { return kind; }
  
private:
  const Kind kind;
};

enum LengthModifier {
 None,
 AsChar,      // 'hh'
 AsShort,     // 'h'
 AsLong,      // 'l'
 AsLongLong,  // 'll'
 AsIntMax,    // 'j'
 AsSizeT,     // 'z'
 AsPtrDiff,   // 't'
 AsLongDouble // 'L'
};

enum Flags {
 LeftJustified = 0x1,
 PlusPrefix = 0x2,
 SpacePrefix = 0x4,
 AlternativeForm = 0x8,
 LeadingZeroes = 0x16
};

class OptionalAmount {
public:
  enum HowSpecified { NotSpecified, Constant, Arg };

  OptionalAmount(HowSpecified h = NotSpecified) : hs(h), amt(0) {}
  OptionalAmount(unsigned i) : hs(Constant), amt(i) {}

  HowSpecified getHowSpecified() const { return hs; }

  unsigned getConstantAmount() const { 
    assert(hs == Constant);
    return amt;
  }

  unsigned getArgumentsConsumed() {
    return hs == Arg ? 1 : 0;
  }
  
private:
  HowSpecified hs;
  unsigned amt;
};

class FormatSpecifier {
  unsigned conversionSpecifier : 6;
  unsigned lengthModifier : 5;
  unsigned flags : 5;
  OptionalAmount FieldWidth;
  OptionalAmount Precision;
public:
  FormatSpecifier() : conversionSpecifier(0), lengthModifier(0), flags(0) {}
  
  static FormatSpecifier Parse(const char *beg, const char *end);

  // Methods for incrementally constructing the FormatSpecifier.
  void setConversionSpecifier(ConversionSpecifier cs) {
    conversionSpecifier = (unsigned) cs.getKind();
  }
  void setLengthModifier(LengthModifier lm) {
    lengthModifier = (unsigned) lm;
  }
  void setIsLeftJustified() { flags |= LeftJustified; }
  void setHasPlusPrefix() { flags |= PlusPrefix; }
  void setHasSpacePrefix() { flags |= SpacePrefix; }
  void setHasAlternativeForm() { flags |= AlternativeForm; }
  void setHasLeadingZeros() { flags |= LeadingZeroes; }

  // Methods for querying the format specifier.

  ConversionSpecifier getConversionSpecifier() const {
    return (ConversionSpecifier::Kind) conversionSpecifier;
  }

  LengthModifier getLengthModifier() const {
    return (LengthModifier) lengthModifier;
  }
  
  void setFieldWidth(const OptionalAmount &Amt) {
    FieldWidth = Amt;
  }
  
  void setPrecision(const OptionalAmount &Amt) {
    Precision = Amt;
  }

  bool isLeftJustified() const { return flags & LeftJustified; }
  bool hasPlusPrefix() const { return flags & PlusPrefix; }
  bool hasAlternativeForm() const { return flags & AlternativeForm; }
  bool hasLeadingZeros() const { return flags & LeadingZeroes; }
};

  
class FormatStringHandler {
public:
  FormatStringHandler() {}
  virtual ~FormatStringHandler();
  
  virtual void HandleIncompleteFormatSpecifier(const char *startSpecifier,
                                               const char *endSpecifier) {}

  virtual void HandleNullChar(const char *nullCharacter) {}
  
  virtual void HandleIncompletePrecision(const char *periodChar) {}
  
  virtual void HandleInvalidConversionSpecifier(const char *conversionChar) {}
  
  virtual void HandleFormatSpecifier(const FormatSpecifier &FS,
                                     const char *startSpecifier,
                                     const char *endSpecifier) {}
};
  
bool ParseFormatString(FormatStringHandler &H,
                       const char *beg, const char *end);


} // end printf namespace
} // end clang namespace
#endif