Coverage Report

Created: 2020-02-25 14:32

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/AST/PrintfFormatString.cpp
Line
Count
Source (jump to first uncovered line)
1
//== PrintfFormatString.cpp - Analysis of printf format strings --*- 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
// Handling of format string in printf and friends.  The structure of format
10
// strings for fprintf() are described in C99 7.19.6.1.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "clang/AST/FormatString.h"
15
#include "clang/AST/OSLog.h"
16
#include "FormatStringParsing.h"
17
#include "clang/Basic/TargetInfo.h"
18
19
using clang::analyze_format_string::ArgType;
20
using clang::analyze_format_string::FormatStringHandler;
21
using clang::analyze_format_string::LengthModifier;
22
using clang::analyze_format_string::OptionalAmount;
23
using clang::analyze_format_string::ConversionSpecifier;
24
using clang::analyze_printf::PrintfSpecifier;
25
26
using namespace clang;
27
28
typedef clang::analyze_format_string::SpecifierResult<PrintfSpecifier>
29
        PrintfSpecifierResult;
30
31
//===----------------------------------------------------------------------===//
32
// Methods for parsing format strings.
33
//===----------------------------------------------------------------------===//
34
35
using analyze_format_string::ParseNonPositionAmount;
36
37
static bool ParsePrecision(FormatStringHandler &H, PrintfSpecifier &FS,
38
                           const char *Start, const char *&Beg, const char *E,
39
245
                           unsigned *argIndex) {
40
245
  if (argIndex) {
41
237
    FS.setPrecision(ParseNonPositionAmount(Beg, E, *argIndex));
42
237
  } else {
43
8
    const OptionalAmount Amt = ParsePositionAmount(H, Start, Beg, E,
44
8
                                           analyze_format_string::PrecisionPos);
45
8
    if (Amt.isInvalid())
46
0
      return true;
47
8
    FS.setPrecision(Amt);
48
8
  }
49
245
  return false;
50
245
}
51
52
static bool ParseObjCFlags(FormatStringHandler &H, PrintfSpecifier &FS,
53
17
                           const char *FlagBeg, const char *E, bool Warn) {
54
17
   StringRef Flag(FlagBeg, E - FlagBeg);
55
17
   // Currently there is only one flag.
56
17
   if (Flag == "tt") {
57
13
     FS.setHasObjCTechnicalTerm(FlagBeg);
58
13
     return false;
59
13
   }
60
4
   // Handle either the case of no flag or an invalid flag.
61
4
   if (Warn) {
62
2
     if (Flag == "")
63
1
       H.HandleEmptyObjCModifierFlag(FlagBeg, E  - FlagBeg);
64
1
     else
65
1
       H.HandleInvalidObjCModifierFlag(FlagBeg, E  - FlagBeg);
66
2
   }
67
4
   return true;
68
4
}
69
70
static PrintfSpecifierResult ParsePrintfSpecifier(FormatStringHandler &H,
71
                                                  const char *&Beg,
72
                                                  const char *E,
73
                                                  unsigned &argIndex,
74
                                                  const LangOptions &LO,
75
                                                  const TargetInfo &Target,
76
                                                  bool Warn,
77
48.4k
                                                  bool isFreeBSDKPrintf) {
78
48.4k
79
48.4k
  using namespace clang::analyze_format_string;
80
48.4k
  using namespace clang::analyze_printf;
81
48.4k
82
48.4k
  const char *I = Beg;
83
48.4k
  const char *Start = nullptr;
84
48.4k
  UpdateOnReturn <const char*> UpdateBeg(Beg, I);
85
48.4k
86
48.4k
  // Look for a '%' character that indicates the start of a format specifier.
87
656k
  for ( ; I != E ; 
++I608k
) {
88
639k
    char c = *I;
89
639k
    if (c == '\0') {
90
38
      // Detect spurious null characters, which are likely errors.
91
38
      H.HandleNullChar(I);
92
38
      return true;
93
38
    }
94
639k
    if (c == '%') {
95
31.0k
      Start = I++;  // Record the start of the format specifier.
96
31.0k
      break;
97
31.0k
    }
98
639k
  }
99
48.4k
100
48.4k
  // No format specifier found?
101
48.4k
  
if (48.3k
!Start48.3k
)
102
17.3k
    return false;
103
31.0k
104
31.0k
  if (I == E) {
105
21
    // No more characters left?
106
21
    if (Warn)
107
21
      H.HandleIncompleteSpecifier(Start, E - Start);
108
21
    return true;
109
21
  }
110
31.0k
111
31.0k
  PrintfSpecifier FS;
112
31.0k
  if (ParseArgPosition(H, FS, Start, I, E))
113
6
    return true;
114
31.0k
115
31.0k
  if (I == E) {
116
0
    // No more characters left?
117
0
    if (Warn)
118
0
      H.HandleIncompleteSpecifier(Start, E - Start);
119
0
    return true;
120
0
  }
121
31.0k
122
31.0k
  if (*I == '{') {
123
84
    ++I;
124
84
    unsigned char PrivacyFlags = 0;
125
84
    StringRef MatchedStr;
126
84
127
128
    do {
128
128
      StringRef Str(I, E - I);
129
128
      std::string Match = "^[[:space:]]*"
130
128
                          "(private|public|sensitive|mask\\.[^[:space:],}]*)"
131
128
                          "[[:space:]]*(,|})";
132
128
      llvm::Regex R(Match);
133
128
      SmallVector<StringRef, 2> Matches;
134
128
135
128
      if (R.match(Str, &Matches)) {
136
96
        MatchedStr = Matches[1];
137
96
        I += Matches[0].size();
138
96
139
96
        // Set the privacy flag if the privacy annotation in the
140
96
        // comma-delimited segment is at least as strict as the privacy
141
96
        // annotations in previous comma-delimited segments.
142
96
        if (MatchedStr.startswith("mask")) {
143
17
          StringRef MaskType = MatchedStr.substr(sizeof("mask.") - 1);
144
17
          unsigned Size = MaskType.size();
145
17
          if (Warn && (Size == 0 || 
Size > 816
))
146
2
            H.handleInvalidMaskType(MaskType);
147
17
          FS.setMaskType(MaskType);
148
79
        } else if (MatchedStr.equals("sensitive"))
149
8
          PrivacyFlags = clang::analyze_os_log::OSLogBufferItem::IsSensitive;
150
71
        else if (PrivacyFlags !=
151
71
                 clang::analyze_os_log::OSLogBufferItem::IsSensitive &&
152
71
                 
MatchedStr.equals("private")63
)
153
33
          PrivacyFlags = clang::analyze_os_log::OSLogBufferItem::IsPrivate;
154
38
        else if (PrivacyFlags == 0 && 
MatchedStr.equals("public")22
)
155
22
          PrivacyFlags = clang::analyze_os_log::OSLogBufferItem::IsPublic;
156
96
      } else {
157
32
        size_t CommaOrBracePos =
158
284
            Str.find_if([](char c) { return c == ',' || 
c == '}'272
; });
159
32
160
32
        if (CommaOrBracePos == StringRef::npos) {
161
0
          // Neither a comma nor the closing brace was found.
162
0
          if (Warn)
163
0
            H.HandleIncompleteSpecifier(Start, E - Start);
164
0
          return true;
165
0
        }
166
32
167
32
        I += CommaOrBracePos + 1;
168
32
      }
169
128
      // Continue until the closing brace is found.
170
128
    } while (*(I - 1) == ',');
171
84
172
84
    // Set the privacy flag.
173
84
    switch (PrivacyFlags) {
174
29
    case 0:
175
29
      break;
176
25
    case clang::analyze_os_log::OSLogBufferItem::IsPrivate:
177
25
      FS.setIsPrivate(MatchedStr.data());
178
25
      break;
179
22
    case clang::analyze_os_log::OSLogBufferItem::IsPublic:
180
22
      FS.setIsPublic(MatchedStr.data());
181
22
      break;
182
8
    case clang::analyze_os_log::OSLogBufferItem::IsSensitive:
183
8
      FS.setIsSensitive(MatchedStr.data());
184
8
      break;
185
0
    default:
186
0
      llvm_unreachable("Unexpected privacy flag value");
187
31.0k
    }
188
31.0k
  }
189
31.0k
190
31.0k
  // Look for flags (if any).
191
31.0k
  bool hasMore = true;
192
31.3k
  for ( ; I != E; 
++I318
) {
193
31.3k
    switch (*I) {
194
31.0k
      default: hasMore = false; break;
195
14
      case '\'':
196
14
        // FIXME: POSIX specific.  Always accept?
197
14
        FS.setHasThousandsGrouping(I);
198
14
        break;
199
20
      case '-': FS.setIsLeftJustified(I); break;
200
115
      case '+': FS.setHasPlusPrefix(I); break;
201
59
      case ' ': FS.setHasSpacePrefix(I); break;
202
67
      case '#': FS.setHasAlternativeForm(I); break;
203
43
      case '0': FS.setHasLeadingZeros(I); break;
204
31.3k
    }
205
31.3k
    if (!hasMore)
206
31.0k
      break;
207
31.3k
  }
208
31.0k
209
31.0k
  if (I == E) {
210
0
    // No more characters left?
211
0
    if (Warn)
212
0
      H.HandleIncompleteSpecifier(Start, E - Start);
213
0
    return true;
214
0
  }
215
31.0k
216
31.0k
  // Look for the field width (if any).
217
31.0k
  if (ParseFieldWidth(H, FS, Start, I, E,
218
31.0k
                      FS.usesPositionalArg() ? 
nullptr55
:
&argIndex30.9k
))
219
2
    return true;
220
31.0k
221
31.0k
  if (I == E) {
222
0
    // No more characters left?
223
0
    if (Warn)
224
0
      H.HandleIncompleteSpecifier(Start, E - Start);
225
0
    return true;
226
0
  }
227
31.0k
228
31.0k
  // Look for the precision (if any).
229
31.0k
  if (*I == '.') {
230
251
    ++I;
231
251
    if (I == E) {
232
6
      if (Warn)
233
6
        H.HandleIncompleteSpecifier(Start, E - Start);
234
6
      return true;
235
6
    }
236
245
237
245
    if (ParsePrecision(H, FS, Start, I, E,
238
245
                       FS.usesPositionalArg() ? 
nullptr8
:
&argIndex237
))
239
0
      return true;
240
245
241
245
    if (I == E) {
242
0
      // No more characters left?
243
0
      if (Warn)
244
0
        H.HandleIncompleteSpecifier(Start, E - Start);
245
0
      return true;
246
0
    }
247
31.0k
  }
248
31.0k
249
31.0k
  if (ParseVectorModifier(H, FS, I, E, LO))
250
12
    return true;
251
30.9k
252
30.9k
  // Look for the length modifier.
253
30.9k
  if (ParseLengthModifier(FS, I, E, LO) && 
I == E6.16k
) {
254
2
    // No more characters left?
255
2
    if (Warn)
256
2
      H.HandleIncompleteSpecifier(Start, E - Start);
257
2
    return true;
258
2
  }
259
30.9k
260
30.9k
  // Look for the Objective-C modifier flags, if any.
261
30.9k
  // We parse these here, even if they don't apply to
262
30.9k
  // the conversion specifier, and then emit an error
263
30.9k
  // later if the conversion specifier isn't '@'.  This
264
30.9k
  // enables better recovery, and we don't know if
265
30.9k
  // these flags are applicable until later.
266
30.9k
  const char *ObjCModifierFlagsStart = nullptr,
267
30.9k
             *ObjCModifierFlagsEnd = nullptr;
268
30.9k
  if (*I == '[') {
269
21
    ObjCModifierFlagsStart = I;
270
21
    ++I;
271
21
    auto flagStart = I;
272
61
    for (;; 
++I40
) {
273
61
      ObjCModifierFlagsEnd = I;
274
61
      if (I == E) {
275
4
        if (Warn)
276
2
          H.HandleIncompleteSpecifier(Start, E - Start);
277
4
        return true;
278
4
      }
279
57
      // Did we find the closing ']'?
280
57
      if (*I == ']') {
281
17
        if (ParseObjCFlags(H, FS, flagStart, I, Warn))
282
4
          return true;
283
13
        ++I;
284
13
        break;
285
13
      }
286
57
      // There are no separators defined yet for multiple
287
57
      // Objective-C modifier flags.  When those are
288
57
      // defined, this is the place to check.
289
57
    }
290
21
  }
291
30.9k
292
30.9k
  
if (30.9k
*I == '\0'30.9k
) {
293
2
    // Detect spurious null characters, which are likely errors.
294
2
    H.HandleNullChar(I);
295
2
    return true;
296
2
  }
297
30.9k
298
30.9k
  // Finally, look for the conversion specifier.
299
30.9k
  const char *conversionPosition = I++;
300
30.9k
  ConversionSpecifier::Kind k = ConversionSpecifier::InvalidSpecifier;
301
30.9k
  switch (*conversionPosition) {
302
48
    default:
303
48
      break;
304
0
    // C99: 7.19.6.1 (section 8).
305
82
    case '%': k = ConversionSpecifier::PercentArg;   break;
306
5
    case 'A': k = ConversionSpecifier::AArg; break;
307
5
    case 'E': k = ConversionSpecifier::EArg; break;
308
5
    case 'F': k = ConversionSpecifier::FArg; break;
309
5
    case 'G': k = ConversionSpecifier::GArg; break;
310
41
    case 'X': k = ConversionSpecifier::XArg; break;
311
16
    case 'a': k = ConversionSpecifier::aArg; break;
312
1.37k
    case 'c': k = ConversionSpecifier::cArg; break;
313
11.9k
    case 'd': k = ConversionSpecifier::dArg; break;
314
53
    case 'e': k = ConversionSpecifier::eArg; break;
315
394
    case 'f': k = ConversionSpecifier::fArg; break;
316
1.17k
    case 'g': k = ConversionSpecifier::gArg; break;
317
771
    case 'i': k = ConversionSpecifier::iArg; break;
318
80
    case 'n':
319
80
      // Not handled, but reserved in OpenCL.
320
80
      if (!LO.OpenCL)
321
76
        k = ConversionSpecifier::nArg;
322
80
      break;
323
27
    case 'o': k = ConversionSpecifier::oArg; break;
324
3.77k
    case 'p': k = ConversionSpecifier::pArg; break;
325
5.24k
    case 's': k = ConversionSpecifier::sArg; break;
326
2.96k
    case 'u': k = ConversionSpecifier::uArg; break;
327
195
    case 'x': k = ConversionSpecifier::xArg; break;
328
0
    // POSIX specific.
329
215
    case 'C': k = ConversionSpecifier::CArg; break;
330
297
    case 'S': k = ConversionSpecifier::SArg; break;
331
0
    // Apple extension for os_log
332
21
    case 'P':
333
21
      k = ConversionSpecifier::PArg;
334
21
      break;
335
0
    // Objective-C.
336
2.08k
    case '@': k = ConversionSpecifier::ObjCObjArg; break;
337
0
    // Glibc specific.
338
13
    case 'm': k = ConversionSpecifier::PrintErrno; break;
339
0
    // FreeBSD kernel specific.
340
18
    case 'b':
341
18
      if (isFreeBSDKPrintf)
342
15
        k = ConversionSpecifier::FreeBSDbArg; // int followed by char *
343
18
      break;
344
18
    case 'r':
345
18
      if (isFreeBSDKPrintf)
346
18
        k = ConversionSpecifier::FreeBSDrArg; // int
347
18
      break;
348
22
    case 'y':
349
22
      if (isFreeBSDKPrintf)
350
18
        k = ConversionSpecifier::FreeBSDyArg; // int
351
22
      break;
352
0
    // Apple-specific.
353
58
    case 'D':
354
58
      if (isFreeBSDKPrintf)
355
30
        k = ConversionSpecifier::FreeBSDDArg; // void * followed by char *
356
28
      else if (Target.getTriple().isOSDarwin())
357
20
        k = ConversionSpecifier::DArg;
358
58
      break;
359
22
    case 'O':
360
22
      if (Target.getTriple().isOSDarwin())
361
14
        k = ConversionSpecifier::OArg;
362
22
      break;
363
22
    case 'U':
364
22
      if (Target.getTriple().isOSDarwin())
365
14
        k = ConversionSpecifier::UArg;
366
22
      break;
367
0
    // MS specific.
368
10
    case 'Z':
369
10
      if (Target.getTriple().isOSMSVCRT())
370
6
        k = ConversionSpecifier::ZArg;
371
10
      break;
372
30.9k
  }
373
30.9k
374
30.9k
  // Check to see if we used the Objective-C modifier flags with
375
30.9k
  // a conversion specifier other than '@'.
376
30.9k
  if (k != ConversionSpecifier::ObjCObjArg &&
377
30.9k
      
k != ConversionSpecifier::InvalidSpecifier28.8k
&&
378
30.9k
      
ObjCModifierFlagsStart28.8k
) {
379
2
    H.HandleObjCFlagsWithNonObjCConversion(ObjCModifierFlagsStart,
380
2
                                           ObjCModifierFlagsEnd + 1,
381
2
                                           conversionPosition);
382
2
    return true;
383
2
  }
384
30.9k
385
30.9k
  PrintfConversionSpecifier CS(conversionPosition, k);
386
30.9k
  FS.setConversionSpecifier(CS);
387
30.9k
  if (CS.consumesDataArgument() && 
!FS.usesPositionalArg()30.7k
)
388
30.7k
    FS.setArgIndex(argIndex++);
389
30.9k
  // FreeBSD kernel specific.
390
30.9k
  if (k == ConversionSpecifier::FreeBSDbArg ||
391
30.9k
      
k == ConversionSpecifier::FreeBSDDArg30.9k
)
392
45
    argIndex++;
393
30.9k
394
30.9k
  if (k == ConversionSpecifier::InvalidSpecifier) {
395
87
    unsigned Len = I - Start;
396
87
    if (ParseUTF8InvalidSpecifier(Start, E, Len)) {
397
12
      CS.setEndScanList(Start + Len);
398
12
      FS.setConversionSpecifier(CS);
399
12
    }
400
87
    // Assume the conversion takes one argument.
401
87
    return !H.HandleInvalidPrintfConversionSpecifier(FS, Start, Len);
402
87
  }
403
30.8k
  return PrintfSpecifierResult(Start, FS);
404
30.8k
}
405
406
bool clang::analyze_format_string::ParsePrintfString(FormatStringHandler &H,
407
                                                     const char *I,
408
                                                     const char *E,
409
                                                     const LangOptions &LO,
410
                                                     const TargetInfo &Target,
411
20.2k
                                                     bool isFreeBSDKPrintf) {
412
20.2k
413
20.2k
  unsigned argIndex = 0;
414
20.2k
415
20.2k
  // Keep looking for a format specifier until we have exhausted the string.
416
66.1k
  while (I != E) {
417
46.0k
    const PrintfSpecifierResult &FSR = ParsePrintfSpecifier(H, I, E, argIndex,
418
46.0k
                                                            LO, Target, true,
419
46.0k
                                                            isFreeBSDKPrintf);
420
46.0k
    // Did a fail-stop error of any kind occur when parsing the specifier?
421
46.0k
    // If so, don't do any more processing.
422
46.0k
    if (FSR.shouldStop())
423
108
      return true;
424
45.9k
    // Did we exhaust the string or encounter an error that
425
45.9k
    // we can recover from?
426
45.9k
    if (!FSR.hasValue())
427
16.9k
      continue;
428
28.9k
    // We have a format specifier.  Pass it to the callback.
429
28.9k
    if (!H.HandlePrintfSpecifier(FSR.getValue(), FSR.getStart(),
430
28.9k
                                 I - FSR.getStart()))
431
100
      return true;
432
28.9k
  }
433
20.2k
  assert(I == E && "Format string not exhausted");
434
20.0k
  return false;
435
20.2k
}
436
437
bool clang::analyze_format_string::ParseFormatStringHasSArg(const char *I,
438
                                                            const char *E,
439
                                                            const LangOptions &LO,
440
1.33k
                                                            const TargetInfo &Target) {
441
1.33k
442
1.33k
  unsigned argIndex = 0;
443
1.33k
444
1.33k
  // Keep looking for a %s format specifier until we have exhausted the string.
445
1.33k
  FormatStringHandler H;
446
3.53k
  while (I != E) {
447
2.34k
    const PrintfSpecifierResult &FSR = ParsePrintfSpecifier(H, I, E, argIndex,
448
2.34k
                                                            LO, Target, false,
449
2.34k
                                                            false);
450
2.34k
    // Did a fail-stop error of any kind occur when parsing the specifier?
451
2.34k
    // If so, don't do any more processing.
452
2.34k
    if (FSR.shouldStop())
453
5
      return false;
454
2.34k
    // Did we exhaust the string or encounter an error that
455
2.34k
    // we can recover from?
456
2.34k
    if (!FSR.hasValue())
457
452
      continue;
458
1.89k
    const analyze_printf::PrintfSpecifier &FS = FSR.getValue();
459
1.89k
    // Return true if this a %s format specifier.
460
1.89k
    if (FS.getConversionSpecifier().getKind() == ConversionSpecifier::Kind::sArg)
461
147
      return true;
462
1.89k
  }
463
1.33k
  
return false1.18k
;
464
1.33k
}
465
466
bool clang::analyze_format_string::parseFormatStringHasFormattingSpecifiers(
467
    const char *Begin, const char *End, const LangOptions &LO,
468
3
    const TargetInfo &Target) {
469
3
  unsigned ArgIndex = 0;
470
3
  // Keep looking for a formatting specifier until we have exhausted the string.
471
3
  FormatStringHandler H;
472
4
  while (Begin != End) {
473
3
    const PrintfSpecifierResult &FSR =
474
3
        ParsePrintfSpecifier(H, Begin, End, ArgIndex, LO, Target, false, false);
475
3
    if (FSR.shouldStop())
476
0
      break;
477
3
    if (FSR.hasValue())
478
2
      return true;
479
3
  }
480
3
  
return false1
;
481
3
}
482
483
//===----------------------------------------------------------------------===//
484
// Methods on PrintfSpecifier.
485
//===----------------------------------------------------------------------===//
486
487
ArgType PrintfSpecifier::getScalarArgType(ASTContext &Ctx,
488
25.8k
                                          bool IsObjCLiteral) const {
489
25.8k
  if (CS.getKind() == ConversionSpecifier::cArg)
490
1.33k
    switch (LM.getKind()) {
491
1.31k
      case LengthModifier::None:
492
1.31k
        return Ctx.IntTy;
493
19
      case LengthModifier::AsLong:
494
19
      case LengthModifier::AsWide:
495
19
        return ArgType(ArgType::WIntTy, "wint_t");
496
19
      case LengthModifier::AsShort:
497
1
        if (Ctx.getTargetInfo().getTriple().isOSMSVCRT())
498
1
          return Ctx.IntTy;
499
0
        LLVM_FALLTHROUGH;
500
6
      default:
501
6
        return ArgType::Invalid();
502
24.5k
    }
503
24.5k
504
24.5k
  if (CS.isIntArg())
505
10.1k
    switch (LM.getKind()) {
506
17
      case LengthModifier::AsLongDouble:
507
17
        // GNU extension.
508
17
        return Ctx.LongLongTy;
509
7.55k
      case LengthModifier::None:
510
7.55k
      case LengthModifier::AsShortLong:
511
7.55k
        return Ctx.IntTy;
512
7.55k
      case LengthModifier::AsInt32:
513
2
        return ArgType(Ctx.IntTy, "__int32");
514
7.55k
      case LengthModifier::AsChar:
515
81
        return ArgType::AnyCharTy;
516
7.55k
      
case LengthModifier::AsShort: return Ctx.ShortTy677
;
517
7.55k
      
case LengthModifier::AsLong: return Ctx.LongTy1.16k
;
518
7.55k
      case LengthModifier::AsLongLong:
519
650
      case LengthModifier::AsQuad:
520
650
        return Ctx.LongLongTy;
521
650
      case LengthModifier::AsInt64:
522
2
        return ArgType(Ctx.LongLongTy, "__int64");
523
650
      case LengthModifier::AsIntMax:
524
5
        return ArgType(Ctx.getIntMaxType(), "intmax_t");
525
650
      case LengthModifier::AsSizeT:
526
15
        return ArgType::makeSizeT(ArgType(Ctx.getSignedSizeType(), "ssize_t"));
527
650
      case LengthModifier::AsInt3264:
528
4
        return Ctx.getTargetInfo().getTriple().isArch64Bit()
529
4
                   ? 
ArgType(Ctx.LongLongTy, "__int64")1
530
4
                   : 
ArgType(Ctx.IntTy, "__int32")3
;
531
650
      case LengthModifier::AsPtrDiff:
532
17
        return ArgType::makePtrdiffT(
533
17
            ArgType(Ctx.getPointerDiffType(), "ptrdiff_t"));
534
650
      case LengthModifier::AsAllocate:
535
0
      case LengthModifier::AsMAllocate:
536
0
      case LengthModifier::AsWide:
537
0
        return ArgType::Invalid();
538
14.3k
    }
539
14.3k
540
14.3k
  if (CS.isUIntArg())
541
3.27k
    switch (LM.getKind()) {
542
12
      case LengthModifier::AsLongDouble:
543
12
        // GNU extension.
544
12
        return Ctx.UnsignedLongLongTy;
545
884
      case LengthModifier::None:
546
884
      case LengthModifier::AsShortLong:
547
884
        return Ctx.UnsignedIntTy;
548
884
      case LengthModifier::AsInt32:
549
1
        return ArgType(Ctx.UnsignedIntTy, "unsigned __int32");
550
884
      
case LengthModifier::AsChar: return Ctx.UnsignedCharTy38
;
551
884
      
case LengthModifier::AsShort: return Ctx.UnsignedShortTy777
;
552
884
      
case LengthModifier::AsLong: return Ctx.UnsignedLongTy704
;
553
884
      case LengthModifier::AsLongLong:
554
654
      case LengthModifier::AsQuad:
555
654
        return Ctx.UnsignedLongLongTy;
556
654
      case LengthModifier::AsInt64:
557
1
        return ArgType(Ctx.UnsignedLongLongTy, "unsigned __int64");
558
654
      case LengthModifier::AsIntMax:
559
5
        return ArgType(Ctx.getUIntMaxType(), "uintmax_t");
560
654
      case LengthModifier::AsSizeT:
561
188
        return ArgType::makeSizeT(ArgType(Ctx.getSizeType(), "size_t"));
562
654
      case LengthModifier::AsInt3264:
563
1
        return Ctx.getTargetInfo().getTriple().isArch64Bit()
564
1
                   ? 
ArgType(Ctx.UnsignedLongLongTy, "unsigned __int64")0
565
1
                   : ArgType(Ctx.UnsignedIntTy, "unsigned __int32");
566
654
      case LengthModifier::AsPtrDiff:
567
12
        return ArgType::makePtrdiffT(
568
12
            ArgType(Ctx.getUnsignedPointerDiffType(), "unsigned ptrdiff_t"));
569
654
      case LengthModifier::AsAllocate:
570
0
      case LengthModifier::AsMAllocate:
571
0
      case LengthModifier::AsWide:
572
0
        return ArgType::Invalid();
573
11.0k
    }
574
11.0k
575
11.0k
  if (CS.isDoubleArg()) {
576
1.59k
    if (!VectorNumElts.isInvalid()) {
577
78
      switch (LM.getKind()) {
578
5
      case LengthModifier::AsShort:
579
5
        return Ctx.HalfTy;
580
31
      case LengthModifier::AsShortLong:
581
31
        return Ctx.FloatTy;
582
28
      case LengthModifier::AsLong:
583
42
      default:
584
42
        return Ctx.DoubleTy;
585
1.51k
      }
586
1.51k
    }
587
1.51k
588
1.51k
    if (LM.getKind() == LengthModifier::AsLongDouble)
589
45
      return Ctx.LongDoubleTy;
590
1.47k
    return Ctx.DoubleTy;
591
1.47k
  }
592
9.48k
593
9.48k
  if (CS.getKind() == ConversionSpecifier::nArg) {
594
75
    switch (LM.getKind()) {
595
28
      case LengthModifier::None:
596
28
        return ArgType::PtrTo(Ctx.IntTy);
597
8
      case LengthModifier::AsChar:
598
8
        return ArgType::PtrTo(Ctx.SignedCharTy);
599
6
      case LengthModifier::AsShort:
600
6
        return ArgType::PtrTo(Ctx.ShortTy);
601
6
      case LengthModifier::AsLong:
602
6
        return ArgType::PtrTo(Ctx.LongTy);
603
12
      case LengthModifier::AsLongLong:
604
12
      case LengthModifier::AsQuad:
605
12
        return ArgType::PtrTo(Ctx.LongLongTy);
606
12
      case LengthModifier::AsIntMax:
607
3
        return ArgType::PtrTo(ArgType(Ctx.getIntMaxType(), "intmax_t"));
608
12
      case LengthModifier::AsSizeT:
609
3
        return ArgType::PtrTo(ArgType(Ctx.getSignedSizeType(), "ssize_t"));
610
12
      case LengthModifier::AsPtrDiff:
611
7
        return ArgType::PtrTo(ArgType(Ctx.getPointerDiffType(), "ptrdiff_t"));
612
12
      case LengthModifier::AsLongDouble:
613
2
        return ArgType(); // FIXME: Is this a known extension?
614
12
      case LengthModifier::AsAllocate:
615
0
      case LengthModifier::AsMAllocate:
616
0
      case LengthModifier::AsInt32:
617
0
      case LengthModifier::AsInt3264:
618
0
      case LengthModifier::AsInt64:
619
0
      case LengthModifier::AsWide:
620
0
        return ArgType::Invalid();
621
0
      case LengthModifier::AsShortLong:
622
0
        llvm_unreachable("only used for OpenCL which doesn not handle nArg");
623
9.41k
    }
624
9.41k
  }
625
9.41k
626
9.41k
  switch (CS.getKind()) {
627
4.34k
    case ConversionSpecifier::sArg:
628
4.34k
      if (LM.getKind() == LengthModifier::AsWideChar) {
629
103
        if (IsObjCLiteral)
630
2
          return ArgType(Ctx.getPointerType(Ctx.UnsignedShortTy.withConst()),
631
2
                         "const unichar *");
632
101
        return ArgType(ArgType::WCStrTy, "wchar_t *");
633
101
      }
634
4.24k
      if (LM.getKind() == LengthModifier::AsWide)
635
2
        return ArgType(ArgType::WCStrTy, "wchar_t *");
636
4.24k
      return ArgType::CStrTy;
637
4.24k
    case ConversionSpecifier::SArg:
638
156
      if (IsObjCLiteral)
639
137
        return ArgType(Ctx.getPointerType(Ctx.UnsignedShortTy.withConst()),
640
137
                       "const unichar *");
641
19
      if (Ctx.getTargetInfo().getTriple().isOSMSVCRT() &&
642
19
          
LM.getKind() == LengthModifier::AsShort8
)
643
1
        return ArgType::CStrTy;
644
18
      return ArgType(ArgType::WCStrTy, "wchar_t *");
645
121
    case ConversionSpecifier::CArg:
646
121
      if (IsObjCLiteral)
647
106
        return ArgType(Ctx.UnsignedShortTy, "unichar");
648
15
      if (Ctx.getTargetInfo().getTriple().isOSMSVCRT() &&
649
15
          
LM.getKind() == LengthModifier::AsShort8
)
650
1
        return Ctx.IntTy;
651
14
      return ArgType(Ctx.WideCharTy, "wchar_t");
652
3.73k
    case ConversionSpecifier::pArg:
653
3.73k
    case ConversionSpecifier::PArg:
654
3.73k
      return ArgType::CPointerTy;
655
3.73k
    case ConversionSpecifier::ObjCObjArg:
656
1.04k
      return ArgType::ObjCPointerTy;
657
3.73k
    default:
658
6
      break;
659
6
  }
660
6
661
6
  // FIXME: Handle other cases.
662
6
  return ArgType();
663
6
}
664
665
666
ArgType PrintfSpecifier::getArgType(ASTContext &Ctx,
667
25.8k
                                    bool IsObjCLiteral) const {
668
25.8k
  const PrintfConversionSpecifier &CS = getConversionSpecifier();
669
25.8k
670
25.8k
  if (!CS.consumesDataArgument())
671
0
    return ArgType::Invalid();
672
25.8k
673
25.8k
  ArgType ScalarTy = getScalarArgType(Ctx, IsObjCLiteral);
674
25.8k
  if (!ScalarTy.isValid() || 
VectorNumElts.isInvalid()25.8k
)
675
25.6k
    return ScalarTy;
676
231
677
231
  return ScalarTy.makeVectorType(Ctx, VectorNumElts.getConstantAmount());
678
231
}
679
680
bool PrintfSpecifier::fixType(QualType QT, const LangOptions &LangOpt,
681
4.39k
                              ASTContext &Ctx, bool IsObjCLiteral) {
682
4.39k
  // %n is different from other conversion specifiers; don't try to fix it.
683
4.39k
  if (CS.getKind() == ConversionSpecifier::nArg)
684
26
    return false;
685
4.36k
686
4.36k
  // Handle Objective-C objects first. Note that while the '%@' specifier will
687
4.36k
  // not warn for structure pointer or void pointer arguments (because that's
688
4.36k
  // how CoreFoundation objects are implemented), we only show a fixit for '%@'
689
4.36k
  // if we know it's an object (block, id, class, or __attribute__((NSObject))).
690
4.36k
  if (QT->isObjCRetainableType()) {
691
62
    if (!IsObjCLiteral)
692
18
      return false;
693
44
694
44
    CS.setKind(ConversionSpecifier::ObjCObjArg);
695
44
696
44
    // Disable irrelevant flags
697
44
    HasThousandsGrouping = false;
698
44
    HasPlusPrefix = false;
699
44
    HasSpacePrefix = false;
700
44
    HasAlternativeForm = false;
701
44
    HasLeadingZeroes = false;
702
44
    Precision.setHowSpecified(OptionalAmount::NotSpecified);
703
44
    LM.setKind(LengthModifier::None);
704
44
705
44
    return true;
706
44
  }
707
4.30k
708
4.30k
  // Handle strings next (char *, wchar_t *)
709
4.30k
  if (QT->isPointerType() && 
(QT->getPointeeType()->isAnyCharacterType())3.62k
) {
710
181
    CS.setKind(ConversionSpecifier::sArg);
711
181
712
181
    // Disable irrelevant flags
713
181
    HasAlternativeForm = 0;
714
181
    HasLeadingZeroes = 0;
715
181
716
181
    // Set the long length modifier for wide characters
717
181
    if (QT->getPointeeType()->isWideCharType())
718
0
      LM.setKind(LengthModifier::AsWideChar);
719
181
    else
720
181
      LM.setKind(LengthModifier::None);
721
181
722
181
    return true;
723
181
  }
724
4.12k
725
4.12k
  // If it's an enum, get its underlying type.
726
4.12k
  if (const EnumType *ETy = QT->getAs<EnumType>())
727
0
    QT = ETy->getDecl()->getIntegerType();
728
4.12k
729
4.12k
  const BuiltinType *BT = QT->getAs<BuiltinType>();
730
4.12k
  if (!BT) {
731
3.55k
    const VectorType *VT = QT->getAs<VectorType>();
732
3.55k
    if (VT) {
733
85
      QT = VT->getElementType();
734
85
      BT = QT->getAs<BuiltinType>();
735
85
      VectorNumElts = OptionalAmount(VT->getNumElements());
736
85
    }
737
3.55k
  }
738
4.12k
739
4.12k
  // We can only work with builtin types.
740
4.12k
  if (!BT)
741
3.47k
    return false;
742
652
743
652
  // Set length modifier
744
652
  switch (BT->getKind()) {
745
35
  case BuiltinType::Bool:
746
35
  case BuiltinType::WChar_U:
747
35
  case BuiltinType::WChar_S:
748
35
  case BuiltinType::Char8: // FIXME: Treat like 'char'?
749
35
  case BuiltinType::Char16:
750
35
  case BuiltinType::Char32:
751
35
  case BuiltinType::UInt128:
752
35
  case BuiltinType::Int128:
753
35
  case BuiltinType::Half:
754
35
  case BuiltinType::Float16:
755
35
  case BuiltinType::Float128:
756
35
  case BuiltinType::ShortAccum:
757
35
  case BuiltinType::Accum:
758
35
  case BuiltinType::LongAccum:
759
35
  case BuiltinType::UShortAccum:
760
35
  case BuiltinType::UAccum:
761
35
  case BuiltinType::ULongAccum:
762
35
  case BuiltinType::ShortFract:
763
35
  case BuiltinType::Fract:
764
35
  case BuiltinType::LongFract:
765
35
  case BuiltinType::UShortFract:
766
35
  case BuiltinType::UFract:
767
35
  case BuiltinType::ULongFract:
768
35
  case BuiltinType::SatShortAccum:
769
35
  case BuiltinType::SatAccum:
770
35
  case BuiltinType::SatLongAccum:
771
35
  case BuiltinType::SatUShortAccum:
772
35
  case BuiltinType::SatUAccum:
773
35
  case BuiltinType::SatULongAccum:
774
35
  case BuiltinType::SatShortFract:
775
35
  case BuiltinType::SatFract:
776
35
  case BuiltinType::SatLongFract:
777
35
  case BuiltinType::SatUShortFract:
778
35
  case BuiltinType::SatUFract:
779
35
  case BuiltinType::SatULongFract:
780
35
    // Various types which are non-trivial to correct.
781
35
    return false;
782
35
783
35
#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
784
144
  case BuiltinType::Id:
785
144
#include 
"clang/Basic/OpenCLImageTypes.def"35
786
144
#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
787
144
  
case BuiltinType::Id:48
788
144
#include 
"clang/Basic/OpenCLExtensionTypes.def"4
789
48
#define SVE_TYPE(Name, Id, SingletonId) \
790
48
  case BuiltinType::Id:
791
48
#include 
"clang/Basic/AArch64SVEACLETypes.def"4
792
48
#define SIGNED_TYPE(Id, SingletonId)
793
48
#define UNSIGNED_TYPE(Id, SingletonId)
794
48
#define FLOATING_TYPE(Id, SingletonId)
795
48
#define BUILTIN_TYPE(Id, SingletonId) \
796
72
  case BuiltinType::Id:
797
48
#include 
"clang/AST/BuiltinTypes.def"4
798
4
    // Misc other stuff which doesn't make sense here.
799
4
    return false;
800
68
801
188
  case BuiltinType::UInt:
802
188
  case BuiltinType::Int:
803
188
  case BuiltinType::Float:
804
188
    LM.setKind(VectorNumElts.isInvalid() ?
805
143
               LengthModifier::None : 
LengthModifier::AsShortLong45
);
806
188
    break;
807
188
  case BuiltinType::Double:
808
34
    LM.setKind(VectorNumElts.isInvalid() ?
809
33
               LengthModifier::None : 
LengthModifier::AsLong1
);
810
34
    break;
811
188
  case BuiltinType::Char_U:
812
67
  case BuiltinType::UChar:
813
67
  case BuiltinType::Char_S:
814
67
  case BuiltinType::SChar:
815
67
    LM.setKind(LengthModifier::AsChar);
816
67
    break;
817
67
818
67
  case BuiltinType::Short:
819
66
  case BuiltinType::UShort:
820
66
    LM.setKind(LengthModifier::AsShort);
821
66
    break;
822
66
823
235
  case BuiltinType::Long:
824
235
  case BuiltinType::ULong:
825
235
    LM.setKind(LengthModifier::AsLong);
826
235
    break;
827
235
828
235
  case BuiltinType::LongLong:
829
11
  case BuiltinType::ULongLong:
830
11
    LM.setKind(LengthModifier::AsLongLong);
831
11
    break;
832
11
833
12
  case BuiltinType::LongDouble:
834
12
    LM.setKind(LengthModifier::AsLongDouble);
835
12
    break;
836
613
  }
837
613
838
613
  // Handle size_t, ptrdiff_t, etc. that have dedicated length modifiers in C99.
839
613
  if (isa<TypedefType>(QT) && 
(47
LangOpt.C9947
||
LangOpt.CPlusPlus1116
))
840
47
    namedTypeToLengthModifier(QT, LM);
841
613
842
613
  // If fixing the length modifier was enough, we might be done.
843
613
  if (hasValidLengthModifier(Ctx.getTargetInfo(), LangOpt)) {
844
548
    // If we're going to offer a fix anyway, make sure the sign matches.
845
548
    switch (CS.getKind()) {
846
92
    case ConversionSpecifier::uArg:
847
92
    case ConversionSpecifier::UArg:
848
92
      if (QT->isSignedIntegerType())
849
14
        CS.setKind(clang::analyze_format_string::ConversionSpecifier::dArg);
850
92
      break;
851
182
    case ConversionSpecifier::dArg:
852
182
    case ConversionSpecifier::DArg:
853
182
    case ConversionSpecifier::iArg:
854
182
      if (QT->isUnsignedIntegerType() && 
!HasPlusPrefix24
)
855
21
        CS.setKind(clang::analyze_format_string::ConversionSpecifier::uArg);
856
182
      break;
857
274
    default:
858
274
      // Other specifiers do not have signed/unsigned variants.
859
274
      break;
860
548
    }
861
548
862
548
    const analyze_printf::ArgType &ATR = getArgType(Ctx, IsObjCLiteral);
863
548
    if (ATR.isValid() && 
ATR.matchesType(Ctx, QT)542
)
864
244
      return true;
865
369
  }
866
369
867
369
  // Set conversion specifier and disable any flags which do not apply to it.
868
369
  // Let typedefs to char fall through to int, as %c is silly for uint8_t.
869
369
  if (!isa<TypedefType>(QT) && 
QT->isCharType()342
) {
870
40
    CS.setKind(ConversionSpecifier::cArg);
871
40
    LM.setKind(LengthModifier::None);
872
40
    Precision.setHowSpecified(OptionalAmount::NotSpecified);
873
40
    HasAlternativeForm = 0;
874
40
    HasLeadingZeroes = 0;
875
40
    HasPlusPrefix = 0;
876
40
  }
877
329
  // Test for Floating type first as LongDouble can pass isUnsignedIntegerType
878
329
  else if (QT->isRealFloatingType()) {
879
62
    CS.setKind(ConversionSpecifier::fArg);
880
62
  }
881
267
  else if (QT->isSignedIntegerType()) {
882
198
    CS.setKind(ConversionSpecifier::dArg);
883
198
    HasAlternativeForm = 0;
884
198
  }
885
69
  else if (QT->isUnsignedIntegerType()) {
886
69
    CS.setKind(ConversionSpecifier::uArg);
887
69
    HasAlternativeForm = 0;
888
69
    HasPlusPrefix = 0;
889
69
  } else {
890
0
    llvm_unreachable("Unexpected type");
891
0
  }
892
369
893
369
  return true;
894
369
}
895
896
855
void PrintfSpecifier::toString(raw_ostream &os) const {
897
855
  // Whilst some features have no defined order, we are using the order
898
855
  // appearing in the C99 standard (ISO/IEC 9899:1999 (E) 7.19.6.1)
899
855
  os << "%";
900
855
901
855
  // Positional args
902
855
  if (usesPositionalArg()) {
903
7
    os << getPositionalArgIndex() << "$";
904
7
  }
905
855
906
855
  // Conversion flags
907
855
  if (IsLeftJustified)    
os << "-"0
;
908
855
  if (HasPlusPrefix)      
os << "+"3
;
909
855
  if (HasSpacePrefix)     
os << " "0
;
910
855
  if (HasAlternativeForm) 
os << "#"0
;
911
855
  if (HasLeadingZeroes)   
os << "0"1
;
912
855
913
855
  // Minimum field width
914
855
  FieldWidth.toString(os);
915
855
  // Precision
916
855
  Precision.toString(os);
917
855
918
855
  // Vector modifier
919
855
  if (!VectorNumElts.isInvalid())
920
81
    os << 'v' << VectorNumElts.getConstantAmount();
921
855
922
855
  // Length modifier
923
855
  os << LM.toString();
924
855
  // Conversion specifier
925
855
  os << CS.toString();
926
855
}
927
928
25.4k
bool PrintfSpecifier::hasValidPlusPrefix() const {
929
25.4k
  if (!HasPlusPrefix)
930
25.3k
    return true;
931
67
932
67
  // The plus prefix only makes sense for signed conversions
933
67
  switch (CS.getKind()) {
934
62
  case ConversionSpecifier::dArg:
935
62
  case ConversionSpecifier::DArg:
936
62
  case ConversionSpecifier::iArg:
937
62
  case ConversionSpecifier::fArg:
938
62
  case ConversionSpecifier::FArg:
939
62
  case ConversionSpecifier::eArg:
940
62
  case ConversionSpecifier::EArg:
941
62
  case ConversionSpecifier::gArg:
942
62
  case ConversionSpecifier::GArg:
943
62
  case ConversionSpecifier::aArg:
944
62
  case ConversionSpecifier::AArg:
945
62
  case ConversionSpecifier::FreeBSDrArg:
946
62
  case ConversionSpecifier::FreeBSDyArg:
947
62
    return true;
948
62
949
62
  default:
950
5
    return false;
951
67
  }
952
67
}
953
954
25.4k
bool PrintfSpecifier::hasValidAlternativeForm() const {
955
25.4k
  if (!HasAlternativeForm)
956
25.3k
    return true;
957
43
958
43
  // Alternate form flag only valid with the oxXaAeEfFgG conversions
959
43
  switch (CS.getKind()) {
960
38
  case ConversionSpecifier::oArg:
961
38
  case ConversionSpecifier::OArg:
962
38
  case ConversionSpecifier::xArg:
963
38
  case ConversionSpecifier::XArg:
964
38
  case ConversionSpecifier::aArg:
965
38
  case ConversionSpecifier::AArg:
966
38
  case ConversionSpecifier::eArg:
967
38
  case ConversionSpecifier::EArg:
968
38
  case ConversionSpecifier::fArg:
969
38
  case ConversionSpecifier::FArg:
970
38
  case ConversionSpecifier::gArg:
971
38
  case ConversionSpecifier::GArg:
972
38
  case ConversionSpecifier::FreeBSDrArg:
973
38
  case ConversionSpecifier::FreeBSDyArg:
974
38
    return true;
975
38
976
38
  default:
977
5
    return false;
978
43
  }
979
43
}
980
981
25.4k
bool PrintfSpecifier::hasValidLeadingZeros() const {
982
25.4k
  if (!HasLeadingZeroes)
983
25.3k
    return true;
984
41
985
41
  // Leading zeroes flag only valid with the diouxXaAeEfFgG conversions
986
41
  switch (CS.getKind()) {
987
30
  case ConversionSpecifier::dArg:
988
30
  case ConversionSpecifier::DArg:
989
30
  case ConversionSpecifier::iArg:
990
30
  case ConversionSpecifier::oArg:
991
30
  case ConversionSpecifier::OArg:
992
30
  case ConversionSpecifier::uArg:
993
30
  case ConversionSpecifier::UArg:
994
30
  case ConversionSpecifier::xArg:
995
30
  case ConversionSpecifier::XArg:
996
30
  case ConversionSpecifier::aArg:
997
30
  case ConversionSpecifier::AArg:
998
30
  case ConversionSpecifier::eArg:
999
30
  case ConversionSpecifier::EArg:
1000
30
  case ConversionSpecifier::fArg:
1001
30
  case ConversionSpecifier::FArg:
1002
30
  case ConversionSpecifier::gArg:
1003
30
  case ConversionSpecifier::GArg:
1004
30
  case ConversionSpecifier::FreeBSDrArg:
1005
30
  case ConversionSpecifier::FreeBSDyArg:
1006
30
    return true;
1007
30
1008
30
  default:
1009
11
    return false;
1010
41
  }
1011
41
}
1012
1013
25.4k
bool PrintfSpecifier::hasValidSpacePrefix() const {
1014
25.4k
  if (!HasSpacePrefix)
1015
25.3k
    return true;
1016
35
1017
35
  // The space prefix only makes sense for signed conversions
1018
35
  switch (CS.getKind()) {
1019
31
  case ConversionSpecifier::dArg:
1020
31
  case ConversionSpecifier::DArg:
1021
31
  case ConversionSpecifier::iArg:
1022
31
  case ConversionSpecifier::fArg:
1023
31
  case ConversionSpecifier::FArg:
1024
31
  case ConversionSpecifier::eArg:
1025
31
  case ConversionSpecifier::EArg:
1026
31
  case ConversionSpecifier::gArg:
1027
31
  case ConversionSpecifier::GArg:
1028
31
  case ConversionSpecifier::aArg:
1029
31
  case ConversionSpecifier::AArg:
1030
31
  case ConversionSpecifier::FreeBSDrArg:
1031
31
  case ConversionSpecifier::FreeBSDyArg:
1032
31
    return true;
1033
31
1034
31
  default:
1035
4
    return false;
1036
35
  }
1037
35
}
1038
1039
25.4k
bool PrintfSpecifier::hasValidLeftJustified() const {
1040
25.4k
  if (!IsLeftJustified)
1041
25.4k
    return true;
1042
20
1043
20
  // The left justified flag is valid for all conversions except n
1044
20
  switch (CS.getKind()) {
1045
2
  case ConversionSpecifier::nArg:
1046
2
    return false;
1047
0
1048
18
  default:
1049
18
    return true;
1050
20
  }
1051
20
}
1052
1053
25.4k
bool PrintfSpecifier::hasValidThousandsGroupingPrefix() const {
1054
25.4k
  if (!HasThousandsGrouping)
1055
25.4k
    return true;
1056
14
1057
14
  switch (CS.getKind()) {
1058
12
    case ConversionSpecifier::dArg:
1059
12
    case ConversionSpecifier::DArg:
1060
12
    case ConversionSpecifier::iArg:
1061
12
    case ConversionSpecifier::uArg:
1062
12
    case ConversionSpecifier::UArg:
1063
12
    case ConversionSpecifier::fArg:
1064
12
    case ConversionSpecifier::FArg:
1065
12
    case ConversionSpecifier::gArg:
1066
12
    case ConversionSpecifier::GArg:
1067
12
      return true;
1068
12
    default:
1069
2
      return false;
1070
14
  }
1071
14
}
1072
1073
25.4k
bool PrintfSpecifier::hasValidPrecision() const {
1074
25.4k
  if (Precision.getHowSpecified() == OptionalAmount::NotSpecified)
1075
25.2k
    return true;
1076
181
1077
181
  // Precision is only valid with the diouxXaAeEfFgGsP conversions
1078
181
  switch (CS.getKind()) {
1079
168
  case ConversionSpecifier::dArg:
1080
168
  case ConversionSpecifier::DArg:
1081
168
  case ConversionSpecifier::iArg:
1082
168
  case ConversionSpecifier::oArg:
1083
168
  case ConversionSpecifier::OArg:
1084
168
  case ConversionSpecifier::uArg:
1085
168
  case ConversionSpecifier::UArg:
1086
168
  case ConversionSpecifier::xArg:
1087
168
  case ConversionSpecifier::XArg:
1088
168
  case ConversionSpecifier::aArg:
1089
168
  case ConversionSpecifier::AArg:
1090
168
  case ConversionSpecifier::eArg:
1091
168
  case ConversionSpecifier::EArg:
1092
168
  case ConversionSpecifier::fArg:
1093
168
  case ConversionSpecifier::FArg:
1094
168
  case ConversionSpecifier::gArg:
1095
168
  case ConversionSpecifier::GArg:
1096
168
  case ConversionSpecifier::sArg:
1097
168
  case ConversionSpecifier::FreeBSDrArg:
1098
168
  case ConversionSpecifier::FreeBSDyArg:
1099
168
  case ConversionSpecifier::PArg:
1100
168
    return true;
1101
168
1102
168
  default:
1103
13
    return false;
1104
181
  }
1105
181
}
1106
25.4k
bool PrintfSpecifier::hasValidFieldWidth() const {
1107
25.4k
  if (FieldWidth.getHowSpecified() == OptionalAmount::NotSpecified)
1108
25.2k
      return true;
1109
154
1110
154
  // The field width is valid for all conversions except n
1111
154
  switch (CS.getKind()) {
1112
2
  case ConversionSpecifier::nArg:
1113
2
    return false;
1114
0
1115
152
  default:
1116
152
    return true;
1117
154
  }
1118
154
}