Coverage Report

Created: 2020-11-24 06:42

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/AST/FormatString.cpp
Line
Count
Source (jump to first uncovered line)
1
// FormatString.cpp - Common stuff for handling printf/scanf formats -*- 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
// Shared details for processing format strings of printf and scanf
10
// (and friends).
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "FormatStringParsing.h"
15
#include "clang/Basic/LangOptions.h"
16
#include "clang/Basic/TargetInfo.h"
17
#include "llvm/Support/ConvertUTF.h"
18
19
using clang::analyze_format_string::ArgType;
20
using clang::analyze_format_string::FormatStringHandler;
21
using clang::analyze_format_string::FormatSpecifier;
22
using clang::analyze_format_string::LengthModifier;
23
using clang::analyze_format_string::OptionalAmount;
24
using clang::analyze_format_string::PositionContext;
25
using clang::analyze_format_string::ConversionSpecifier;
26
using namespace clang;
27
28
// Key function to FormatStringHandler.
29
22.0k
FormatStringHandler::~FormatStringHandler() {}
30
31
//===----------------------------------------------------------------------===//
32
// Functions for parsing format strings components in both printf and
33
// scanf format strings.
34
//===----------------------------------------------------------------------===//
35
36
OptionalAmount
37
63.3k
clang::analyze_format_string::ParseAmount(const char *&Beg, const char *E) {
38
63.3k
  const char *I = Beg;
39
63.3k
  UpdateOnReturn <const char*> UpdateBeg(Beg, I);
40
41
63.3k
  unsigned accumulator = 0;
42
63.3k
  bool hasDigits = false;
43
44
64.3k
  for ( ; I != E; 
++I963
) {
45
64.3k
    char c = *I;
46
64.3k
    if (c >= '0' && 
c <= '9'63.4k
) {
47
963
      hasDigits = true;
48
963
      accumulator = (accumulator * 10) + (c - '0');
49
963
      continue;
50
963
    }
51
52
63.3k
    if (hasDigits)
53
876
      return OptionalAmount(OptionalAmount::Constant, accumulator, Beg, I - Beg,
54
876
          false);
55
56
62.5k
    break;
57
62.5k
  }
58
59
62.5k
  return OptionalAmount();
60
63.3k
}
61
62
OptionalAmount
63
clang::analyze_format_string::ParseNonPositionAmount(const char *&Beg,
64
                                                     const char *E,
65
31.0k
                                                     unsigned &argIndex) {
66
31.0k
  if (*Beg == '*') {
67
89
    ++Beg;
68
89
    return OptionalAmount(OptionalAmount::Arg, argIndex++, Beg, 0, false);
69
89
  }
70
71
31.0k
  return ParseAmount(Beg, E);
72
31.0k
}
73
74
OptionalAmount
75
clang::analyze_format_string::ParsePositionAmount(FormatStringHandler &H,
76
                                                  const char *Start,
77
                                                  const char *&Beg,
78
                                                  const char *E,
79
63
                                                  PositionContext p) {
80
63
  if (*Beg == '*') {
81
10
    const char *I = Beg + 1;
82
10
    const OptionalAmount &Amt = ParseAmount(I, E);
83
84
10
    if (Amt.getHowSpecified() == OptionalAmount::NotSpecified) {
85
0
      H.HandleInvalidPosition(Beg, I - Beg, p);
86
0
      return OptionalAmount(false);
87
0
    }
88
89
10
    if (I == E) {
90
      // No more characters left?
91
0
      H.HandleIncompleteSpecifier(Start, E - Start);
92
0
      return OptionalAmount(false);
93
0
    }
94
95
10
    assert(Amt.getHowSpecified() == OptionalAmount::Constant);
96
97
10
    if (*I == '$') {
98
      // Handle positional arguments
99
100
      // Special case: '*0$', since this is an easy mistake.
101
10
      if (Amt.getConstantAmount() == 0) {
102
2
        H.HandleZeroPosition(Beg, I - Beg + 1);
103
2
        return OptionalAmount(false);
104
2
      }
105
106
8
      const char *Tmp = Beg;
107
8
      Beg = ++I;
108
109
8
      return OptionalAmount(OptionalAmount::Arg, Amt.getConstantAmount() - 1,
110
8
                            Tmp, 0, true);
111
8
    }
112
113
0
    H.HandleInvalidPosition(Beg, I - Beg, p);
114
0
    return OptionalAmount(false);
115
0
  }
116
117
53
  return ParseAmount(Beg, E);
118
53
}
119
120
121
bool
122
clang::analyze_format_string::ParseFieldWidth(FormatStringHandler &H,
123
                                              FormatSpecifier &CS,
124
                                              const char *Start,
125
                                              const char *&Beg, const char *E,
126
30.9k
                                              unsigned *argIndex) {
127
  // FIXME: Support negative field widths.
128
30.9k
  if (argIndex) {
129
30.8k
    CS.setFieldWidth(ParseNonPositionAmount(Beg, E, *argIndex));
130
30.8k
  }
131
55
  else {
132
55
    const OptionalAmount Amt =
133
55
      ParsePositionAmount(H, Start, Beg, E,
134
55
                          analyze_format_string::FieldWidthPos);
135
136
55
    if (Amt.isInvalid())
137
2
      return true;
138
53
    CS.setFieldWidth(Amt);
139
53
  }
140
30.9k
  return false;
141
30.9k
}
142
143
bool
144
clang::analyze_format_string::ParseArgPosition(FormatStringHandler &H,
145
                                               FormatSpecifier &FS,
146
                                               const char *Start,
147
                                               const char *&Beg,
148
31.5k
                                               const char *E) {
149
31.5k
  const char *I = Beg;
150
151
31.5k
  const OptionalAmount &Amt = ParseAmount(I, E);
152
153
31.5k
  if (I == E) {
154
    // No more characters left?
155
0
    H.HandleIncompleteSpecifier(Start, E - Start);
156
0
    return true;
157
0
  }
158
159
31.5k
  if (Amt.getHowSpecified() == OptionalAmount::Constant && 
*(I++) == '$'308
) {
160
    // Warn that positional arguments are non-standard.
161
69
    H.HandlePosition(Start, I - Start);
162
163
    // Special case: '%0$', since this is an easy mistake.
164
69
    if (Amt.getConstantAmount() == 0) {
165
6
      H.HandleZeroPosition(Start, I - Start);
166
6
      return true;
167
6
    }
168
169
63
    FS.setArgIndex(Amt.getConstantAmount() - 1);
170
63
    FS.setUsesPositionalArg();
171
    // Update the caller's pointer if we decided to consume
172
    // these characters.
173
63
    Beg = I;
174
63
    return false;
175
63
  }
176
177
31.4k
  return false;
178
31.4k
}
179
180
bool
181
clang::analyze_format_string::ParseVectorModifier(FormatStringHandler &H,
182
                                                  FormatSpecifier &FS,
183
                                                  const char *&I,
184
                                                  const char *E,
185
30.9k
                                                  const LangOptions &LO) {
186
30.9k
  if (!LO.OpenCL)
187
30.7k
    return false;
188
189
174
  const char *Start = I;
190
174
  if (*I == 'v') {
191
168
    ++I;
192
193
168
    if (I == E) {
194
2
      H.HandleIncompleteSpecifier(Start, E - Start);
195
2
      return true;
196
2
    }
197
198
166
    OptionalAmount NumElts = ParseAmount(I, E);
199
166
    if (NumElts.getHowSpecified() != OptionalAmount::Constant) {
200
10
      H.HandleIncompleteSpecifier(Start, E - Start);
201
10
      return true;
202
10
    }
203
204
156
    FS.setVectorNumElts(NumElts);
205
156
  }
206
207
162
  return false;
208
174
}
209
210
bool
211
clang::analyze_format_string::ParseLengthModifier(FormatSpecifier &FS,
212
                                                  const char *&I,
213
                                                  const char *E,
214
                                                  const LangOptions &LO,
215
31.5k
                                                  bool IsScanf) {
216
31.5k
  LengthModifier::Kind lmKind = LengthModifier::None;
217
31.5k
  const char *lmPosition = I;
218
31.5k
  switch (*I) {
219
25.1k
    default:
220
25.1k
      return false;
221
1.62k
    case 'h':
222
1.62k
      ++I;
223
1.62k
      if (I != E && *I == 'h') {
224
124
        ++I;
225
124
        lmKind = LengthModifier::AsChar;
226
1.50k
      } else if (I != E && *I == 'l' && 
LO.OpenCL43
) {
227
39
        ++I;
228
39
        lmKind = LengthModifier::AsShortLong;
229
1.46k
      } else {
230
1.46k
        lmKind = LengthModifier::AsShort;
231
1.46k
      }
232
1.62k
      break;
233
4.20k
    case 'l':
234
4.20k
      ++I;
235
4.20k
      if (I != E && 
*I == 'l'4.19k
) {
236
1.32k
        ++I;
237
1.32k
        lmKind = LengthModifier::AsLongLong;
238
2.87k
      } else {
239
2.87k
        lmKind = LengthModifier::AsLong;
240
2.87k
      }
241
4.20k
      break;
242
19
    case 'j': lmKind = LengthModifier::AsIntMax;     ++I; break;
243
240
    case 'z': lmKind = LengthModifier::AsSizeT;      ++I; break;
244
67
    case 't': lmKind = LengthModifier::AsPtrDiff;    ++I; break;
245
108
    case 'L': lmKind = LengthModifier::AsLongDouble; ++I; break;
246
24
    case 'q': lmKind = LengthModifier::AsQuad;       ++I; break;
247
34
    case 'a':
248
34
      if (IsScanf && 
!LO.C9922
&&
!LO.CPlusPlus1115
) {
249
        // For scanf in C90, look at the next character to see if this should
250
        // be parsed as the GNU extension 'a' length modifier. If not, this
251
        // will be parsed as a conversion specifier.
252
9
        ++I;
253
9
        if (I != E && 
(8
*I == 's'8
||
*I == 'S'5
||
*I == '['4
)) {
254
6
          lmKind = LengthModifier::AsAllocate;
255
6
          break;
256
6
        }
257
3
        --I;
258
3
      }
259
28
      return false;
260
36
    case 'm':
261
36
      if (IsScanf) {
262
23
        lmKind = LengthModifier::AsMAllocate;
263
23
        ++I;
264
23
        break;
265
23
      }
266
13
      return false;
267
    // printf: AsInt64, AsInt32, AsInt3264
268
    // scanf:  AsInt64
269
11
    case 'I':
270
11
      if (I + 1 != E && I + 2 != E) {
271
10
        if (I[1] == '6' && 
I[2] == '4'3
) {
272
3
          I += 3;
273
3
          lmKind = LengthModifier::AsInt64;
274
3
          break;
275
3
        }
276
7
        if (IsScanf)
277
0
          return false;
278
279
7
        if (I[1] == '3' && 
I[2] == '2'3
) {
280
3
          I += 3;
281
3
          lmKind = LengthModifier::AsInt32;
282
3
          break;
283
3
        }
284
5
      }
285
5
      ++I;
286
5
      lmKind = LengthModifier::AsInt3264;
287
5
      break;
288
18
    case 'w':
289
18
      lmKind = LengthModifier::AsWide; ++I; break;
290
6.34k
  }
291
6.34k
  LengthModifier lm(lmPosition, lmKind);
292
6.34k
  FS.setLengthModifier(lm);
293
6.34k
  return true;
294
6.34k
}
295
296
bool clang::analyze_format_string::ParseUTF8InvalidSpecifier(
297
99
    const char *SpecifierBegin, const char *FmtStrEnd, unsigned &Len) {
298
99
  if (SpecifierBegin + 1 >= FmtStrEnd)
299
0
    return false;
300
301
99
  const llvm::UTF8 *SB =
302
99
      reinterpret_cast<const llvm::UTF8 *>(SpecifierBegin + 1);
303
99
  const llvm::UTF8 *SE = reinterpret_cast<const llvm::UTF8 *>(FmtStrEnd);
304
99
  const char FirstByte = *SB;
305
306
  // If the invalid specifier is a multibyte UTF-8 string, return the
307
  // total length accordingly so that the conversion specifier can be
308
  // properly updated to reflect a complete UTF-8 specifier.
309
99
  unsigned NumBytes = llvm::getNumBytesForUTF8(FirstByte);
310
99
  if (NumBytes == 1)
311
79
    return false;
312
20
  if (SB + NumBytes > SE)
313
4
    return false;
314
315
16
  Len = NumBytes + 1;
316
16
  return true;
317
16
}
318
319
//===----------------------------------------------------------------------===//
320
// Methods on ArgType.
321
//===----------------------------------------------------------------------===//
322
323
clang::analyze_format_string::ArgType::MatchKind
324
28.4k
ArgType::matchesType(ASTContext &C, QualType argTy) const {
325
28.4k
  if (Ptr) {
326
    // It has to be a pointer.
327
693
    const PointerType *PT = argTy->getAs<PointerType>();
328
693
    if (!PT)
329
13
      return NoMatch;
330
331
    // We cannot write through a const qualified pointer.
332
680
    if (PT->getPointeeType().isConstQualified())
333
22
      return NoMatch;
334
335
658
    argTy = PT->getPointeeType();
336
658
  }
337
338
28.3k
  switch (K) {
339
0
    case InvalidTy:
340
0
      llvm_unreachable("ArgType must be valid");
341
342
8
    case UnknownTy:
343
8
      return Match;
344
345
237
    case AnyCharTy: {
346
237
      if (const EnumType *ETy = argTy->getAs<EnumType>()) {
347
        // If the enum is incomplete we know nothing about the underlying type.
348
        // Assume that it's 'int'.
349
20
        if (!ETy->getDecl()->isComplete())
350
2
          return NoMatch;
351
18
        argTy = ETy->getDecl()->getIntegerType();
352
18
      }
353
354
235
      if (const BuiltinType *BT = argTy->getAs<BuiltinType>())
355
235
        switch (BT->getKind()) {
356
86
          default:
357
86
            break;
358
149
          case BuiltinType::Char_S:
359
149
          case BuiltinType::SChar:
360
149
          case BuiltinType::UChar:
361
149
          case BuiltinType::Char_U:
362
149
          case BuiltinType::Bool:
363
149
            return Match;
364
86
        }
365
86
      return NoMatch;
366
86
    }
367
368
18.6k
    case SpecificTy: {
369
18.6k
      if (const EnumType *ETy = argTy->getAs<EnumType>()) {
370
        // If the enum is incomplete we know nothing about the underlying type.
371
        // Assume that it's 'int'.
372
54
        if (!ETy->getDecl()->isComplete())
373
4
          argTy = C.IntTy;
374
50
        else
375
50
          argTy = ETy->getDecl()->getIntegerType();
376
54
      }
377
18.6k
      argTy = C.getCanonicalType(argTy).getUnqualifiedType();
378
379
18.6k
      if (T == argTy)
380
15.8k
        return Match;
381
      // Check for "compatible types".
382
2.79k
      if (const BuiltinType *BT = argTy->getAs<BuiltinType>())
383
2.61k
        switch (BT->getKind()) {
384
160
          default:
385
160
            break;
386
96
          case BuiltinType::Char_S:
387
96
          case BuiltinType::SChar:
388
96
          case BuiltinType::Char_U:
389
96
          case BuiltinType::UChar:
390
96
          case BuiltinType::Bool:
391
96
            if (T == C.UnsignedShortTy || 
T == C.ShortTy82
)
392
28
              return NoMatchTypeConfusion;
393
68
            return T == C.UnsignedCharTy || 
T == C.SignedCharTy55
?
Match21
394
47
                                                                : NoMatch;
395
39
          case BuiltinType::Short:
396
31
            return T == C.UnsignedShortTy ? 
Match8
: NoMatch;
397
12
          case BuiltinType::UShort:
398
8
            return T == C.ShortTy ? 
Match4
: NoMatch;
399
1.82k
          case BuiltinType::Int:
400
1.71k
            return T == C.UnsignedIntTy ? 
Match112
: NoMatch;
401
85
          case BuiltinType::UInt:
402
46
            return T == C.IntTy ? 
Match39
: NoMatch;
403
117
          case BuiltinType::Long:
404
81
            return T == C.UnsignedLongTy ? 
Match36
: NoMatch;
405
236
          case BuiltinType::ULong:
406
169
            return T == C.LongTy ? Match : 
NoMatch67
;
407
23
          case BuiltinType::LongLong:
408
14
            return T == C.UnsignedLongLongTy ? Match : 
NoMatch9
;
409
28
          case BuiltinType::ULongLong:
410
24
            return T == C.LongLongTy ? Match : 
NoMatch4
;
411
341
        }
412
341
      return NoMatch;
413
341
    }
414
415
4.54k
    case CStrTy: {
416
4.54k
      const PointerType *PT = argTy->getAs<PointerType>();
417
4.54k
      if (!PT)
418
393
        return NoMatch;
419
4.14k
      QualType pointeeTy = PT->getPointeeType();
420
4.14k
      if (const BuiltinType *BT = pointeeTy->getAs<BuiltinType>())
421
4.14k
        switch (BT->getKind()) {
422
4.13k
          case BuiltinType::Char_U:
423
4.13k
          case BuiltinType::UChar:
424
4.13k
          case BuiltinType::Char_S:
425
4.13k
          case BuiltinType::SChar:
426
4.13k
            return Match;
427
10
          default:
428
10
            break;
429
16
        }
430
431
16
      return NoMatch;
432
16
    }
433
434
130
    case WCStrTy: {
435
130
      const PointerType *PT = argTy->getAs<PointerType>();
436
130
      if (!PT)
437
109
        return NoMatch;
438
21
      QualType pointeeTy =
439
21
        C.getCanonicalType(PT->getPointeeType()).getUnqualifiedType();
440
16
      return pointeeTy == C.getWideCharType() ? Match : 
NoMatch5
;
441
21
    }
442
443
16
    case WIntTy: {
444
16
      QualType WInt = C.getCanonicalType(C.getWIntType()).getUnqualifiedType();
445
446
16
      if (C.getCanonicalType(argTy).getUnqualifiedType() == WInt)
447
8
        return Match;
448
449
8
      QualType PromoArg = argTy->isPromotableIntegerType()
450
0
                              ? C.getPromotedIntegerType(argTy)
451
8
                              : argTy;
452
8
      PromoArg = C.getCanonicalType(PromoArg).getUnqualifiedType();
453
454
      // If the promoted argument is the corresponding signed type of the
455
      // wint_t type, then it should match.
456
8
      if (PromoArg->hasSignedIntegerRepresentation() &&
457
2
          C.getCorrespondingUnsignedType(PromoArg) == WInt)
458
0
        return Match;
459
460
8
      return WInt == PromoArg ? 
Match0
: NoMatch;
461
8
    }
462
463
3.76k
    case CPointerTy:
464
3.76k
      if (argTy->isVoidPointerType()) {
465
137
        return Match;
466
3.63k
      } if (argTy->isPointerType() || 
argTy->isObjCObjectPointerType()54
||
467
3.61k
            
argTy->isBlockPointerType()24
||
argTy->isNullPtrType()22
) {
468
3.61k
        return NoMatchPedantic;
469
20
      } else {
470
20
        return NoMatch;
471
20
      }
472
473
1.07k
    case ObjCPointerTy: {
474
1.07k
      if (argTy->getAs<ObjCObjectPointerType>() ||
475
53
          argTy->getAs<BlockPointerType>())
476
1.02k
        return Match;
477
478
      // Handle implicit toll-free bridging.
479
50
      if (const PointerType *PT = argTy->getAs<PointerType>()) {
480
        // Things such as CFTypeRef are really just opaque pointers
481
        // to C structs representing CF types that can often be bridged
482
        // to Objective-C objects.  Since the compiler doesn't know which
483
        // structs can be toll-free bridged, we just accept them all.
484
12
        QualType pointee = PT->getPointeeType();
485
12
        if (pointee->getAsStructureType() || 
pointee->isVoidType()3
)
486
9
          return Match;
487
41
      }
488
41
      return NoMatch;
489
41
    }
490
0
  }
491
492
0
  llvm_unreachable("Invalid ArgType Kind!");
493
0
}
494
495
231
ArgType ArgType::makeVectorType(ASTContext &C, unsigned NumElts) const {
496
  // Check for valid vector element types.
497
231
  if (T.isNull())
498
20
    return ArgType::Invalid();
499
500
211
  QualType Vec = C.getExtVectorType(T, NumElts);
501
211
  return ArgType(Vec, Name);
502
211
}
503
504
4.40k
QualType ArgType::getRepresentativeType(ASTContext &C) const {
505
4.40k
  QualType Res;
506
4.40k
  switch (K) {
507
0
    case InvalidTy:
508
0
      llvm_unreachable("No representative type for Invalid ArgType");
509
0
    case UnknownTy:
510
0
      llvm_unreachable("No representative type for Unknown ArgType");
511
31
    case AnyCharTy:
512
31
      Res = C.CharTy;
513
31
      break;
514
613
    case SpecificTy:
515
613
      Res = T;
516
613
      break;
517
114
    case CStrTy:
518
114
      Res = C.getPointerType(C.CharTy);
519
114
      break;
520
15
    case WCStrTy:
521
15
      Res = C.getPointerType(C.getWideCharType());
522
15
      break;
523
23
    case ObjCPointerTy:
524
23
      Res = C.ObjCBuiltinIdTy;
525
23
      break;
526
3.60k
    case CPointerTy:
527
3.60k
      Res = C.VoidPtrTy;
528
3.60k
      break;
529
6
    case WIntTy: {
530
6
      Res = C.getWIntType();
531
6
      break;
532
4.40k
    }
533
4.40k
  }
534
535
4.40k
  if (Ptr)
536
176
    Res = C.getPointerType(Res);
537
4.40k
  return Res;
538
4.40k
}
539
540
4.40k
std::string ArgType::getRepresentativeTypeName(ASTContext &C) const {
541
4.40k
  std::string S = getRepresentativeType(C).getAsString(C.getPrintingPolicy());
542
543
4.40k
  std::string Alias;
544
4.40k
  if (Name) {
545
    // Use a specific name for this type, e.g. "size_t".
546
110
    Alias = Name;
547
110
    if (Ptr) {
548
      // If ArgType is actually a pointer to T, append an asterisk.
549
34
      Alias += (Alias[Alias.size()-1] == '*') ? 
"*"5
: " *";
550
39
    }
551
    // If Alias is the same as the underlying type, e.g. wchar_t, then drop it.
552
110
    if (S == Alias)
553
3
      Alias.clear();
554
110
  }
555
556
4.40k
  if (!Alias.empty())
557
107
    return std::string("'") + Alias + "' (aka '" + S + "')";
558
4.30k
  return std::string("'") + S + "'";
559
4.30k
}
560
561
562
//===----------------------------------------------------------------------===//
563
// Methods on OptionalAmount.
564
//===----------------------------------------------------------------------===//
565
566
ArgType
567
60
analyze_format_string::OptionalAmount::getArgType(ASTContext &Ctx) const {
568
60
  return Ctx.IntTy;
569
60
}
570
571
//===----------------------------------------------------------------------===//
572
// Methods on LengthModifier.
573
//===----------------------------------------------------------------------===//
574
575
const char *
576
1.28k
analyze_format_string::LengthModifier::toString() const {
577
1.28k
  switch (kind) {
578
39
  case AsChar:
579
39
    return "hh";
580
71
  case AsShort:
581
71
    return "h";
582
82
  case AsShortLong:
583
82
    return "hl";
584
260
  case AsLong: // or AsWideChar
585
260
    return "l";
586
145
  case AsLongLong:
587
145
    return "ll";
588
24
  case AsQuad:
589
24
    return "q";
590
8
  case AsIntMax:
591
8
    return "j";
592
13
  case AsSizeT:
593
13
    return "z";
594
4
  case AsPtrDiff:
595
4
    return "t";
596
3
  case AsInt32:
597
3
    return "I32";
598
5
  case AsInt3264:
599
5
    return "I";
600
3
  case AsInt64:
601
3
    return "I64";
602
79
  case AsLongDouble:
603
79
    return "L";
604
6
  case AsAllocate:
605
6
    return "a";
606
23
  case AsMAllocate:
607
23
    return "m";
608
18
  case AsWide:
609
18
    return "w";
610
500
  case None:
611
500
    return "";
612
0
  }
613
0
  return nullptr;
614
0
}
615
616
//===----------------------------------------------------------------------===//
617
// Methods on ConversionSpecifier.
618
//===----------------------------------------------------------------------===//
619
620
1.34k
const char *ConversionSpecifier::toString() const {
621
1.34k
  switch (kind) {
622
422
  case dArg: return "d";
623
30
  case DArg: return "D";
624
9
  case iArg: return "i";
625
39
  case oArg: return "o";
626
20
  case OArg: return "O";
627
190
  case uArg: return "u";
628
18
  case UArg: return "U";
629
19
  case xArg: return "x";
630
4
  case XArg: return "X";
631
101
  case fArg: return "f";
632
2
  case FArg: return "F";
633
2
  case eArg: return "e";
634
2
  case EArg: return "E";
635
2
  case gArg: return "g";
636
2
  case GArg: return "G";
637
4
  case aArg: return "a";
638
2
  case AArg: return "A";
639
66
  case cArg: return "c";
640
224
  case sArg: return "s";
641
28
  case pArg: return "p";
642
0
  case PArg:
643
0
    return "P";
644
10
  case nArg: return "n";
645
0
  case PercentArg:  return "%";
646
4
  case ScanListArg: return "[";
647
0
  case InvalidSpecifier: return nullptr;
648
649
  // POSIX unicode extensions.
650
19
  case CArg: return "C";
651
24
  case SArg: return "S";
652
653
  // Objective-C specific specifiers.
654
44
  case ObjCObjArg: return "@";
655
656
  // FreeBSD kernel specific specifiers.
657
0
  case FreeBSDbArg: return "b";
658
0
  case FreeBSDDArg: return "D";
659
27
  case FreeBSDrArg: return "r";
660
27
  case FreeBSDyArg: return "y";
661
662
  // GlibC specific specifiers.
663
0
  case PrintErrno: return "m";
664
665
  // MS specific specifiers.
666
7
  case ZArg: return "Z";
667
0
  }
668
0
  return nullptr;
669
0
}
670
671
Optional<ConversionSpecifier>
672
145
ConversionSpecifier::getStandardSpecifier() const {
673
145
  ConversionSpecifier::Kind NewKind;
674
675
145
  switch (getKind()) {
676
85
  default:
677
85
    return None;
678
24
  case DArg:
679
24
    NewKind = dArg;
680
24
    break;
681
18
  case UArg:
682
18
    NewKind = uArg;
683
18
    break;
684
18
  case OArg:
685
18
    NewKind = oArg;
686
18
    break;
687
60
  }
688
689
60
  ConversionSpecifier FixedCS(*this);
690
60
  FixedCS.setKind(NewKind);
691
60
  return FixedCS;
692
60
}
693
694
//===----------------------------------------------------------------------===//
695
// Methods on OptionalAmount.
696
//===----------------------------------------------------------------------===//
697
698
1.80k
void OptionalAmount::toString(raw_ostream &os) const {
699
1.80k
  switch (hs) {
700
1.78k
  case Invalid:
701
1.78k
  case NotSpecified:
702
1.78k
    return;
703
4
  case Arg:
704
4
    if (UsesDotPrefix)
705
4
        os << ".";
706
4
    if (usesPositionalArg())
707
2
      os << "*" << getPositionalArgIndex() << "$";
708
2
    else
709
2
      os << "*";
710
4
    break;
711
15
  case Constant:
712
15
    if (UsesDotPrefix)
713
8
        os << ".";
714
15
    os << amt;
715
15
    break;
716
1.80k
  }
717
1.80k
}
718
719
bool FormatSpecifier::hasValidLengthModifier(const TargetInfo &Target,
720
26.7k
                                             const LangOptions &LO) const {
721
26.7k
  switch (LM.getKind()) {
722
20.4k
    case LengthModifier::None:
723
20.4k
      return true;
724
725
    // Handle most integer flags
726
1.51k
    case LengthModifier::AsShort:
727
      // Length modifier only applies to FP vectors.
728
1.51k
      if (LO.OpenCL && 
CS.isDoubleArg()37
)
729
6
        return !VectorNumElts.isInvalid();
730
731
1.51k
      if (Target.getTriple().isOSMSVCRT()) {
732
11
        switch (CS.getKind()) {
733
9
          case ConversionSpecifier::cArg:
734
9
          case ConversionSpecifier::CArg:
735
9
          case ConversionSpecifier::sArg:
736
9
          case ConversionSpecifier::SArg:
737
9
          case ConversionSpecifier::ZArg:
738
9
            return true;
739
2
          default:
740
2
            break;
741
1.50k
        }
742
1.50k
      }
743
1.50k
      LLVM_FALLTHROUGH;
744
3.35k
    case LengthModifier::AsChar:
745
3.35k
    case LengthModifier::AsLongLong:
746
3.35k
    case LengthModifier::AsQuad:
747
3.35k
    case LengthModifier::AsIntMax:
748
3.35k
    case LengthModifier::AsSizeT:
749
3.35k
    case LengthModifier::AsPtrDiff:
750
3.35k
      switch (CS.getKind()) {
751
3.24k
        case ConversionSpecifier::dArg:
752
3.24k
        case ConversionSpecifier::DArg:
753
3.24k
        case ConversionSpecifier::iArg:
754
3.24k
        case ConversionSpecifier::oArg:
755
3.24k
        case ConversionSpecifier::OArg:
756
3.24k
        case ConversionSpecifier::uArg:
757
3.24k
        case ConversionSpecifier::UArg:
758
3.24k
        case ConversionSpecifier::xArg:
759
3.24k
        case ConversionSpecifier::XArg:
760
3.24k
        case ConversionSpecifier::nArg:
761
3.24k
          return true;
762
12
        case ConversionSpecifier::FreeBSDrArg:
763
12
        case ConversionSpecifier::FreeBSDyArg:
764
12
          return Target.getTriple().isOSFreeBSD() || 
Target.getTriple().isPS4()4
;
765
100
        default:
766
100
          return false;
767
0
      }
768
769
82
    case LengthModifier::AsShortLong:
770
82
      return LO.OpenCL && !VectorNumElts.isInvalid();
771
772
    // Handle 'l' flag
773
2.71k
    case LengthModifier::AsLong: // or AsWideChar
774
2.71k
      if (CS.isDoubleArg()) {
775
        // Invalid for OpenCL FP scalars.
776
656
        if (LO.OpenCL && 
VectorNumElts.isInvalid()29
)
777
1
          return false;
778
655
        return true;
779
655
      }
780
781
2.06k
      switch (CS.getKind()) {
782
2.03k
        case ConversionSpecifier::dArg:
783
2.03k
        case ConversionSpecifier::DArg:
784
2.03k
        case ConversionSpecifier::iArg:
785
2.03k
        case ConversionSpecifier::oArg:
786
2.03k
        case ConversionSpecifier::OArg:
787
2.03k
        case ConversionSpecifier::uArg:
788
2.03k
        case ConversionSpecifier::UArg:
789
2.03k
        case ConversionSpecifier::xArg:
790
2.03k
        case ConversionSpecifier::XArg:
791
2.03k
        case ConversionSpecifier::nArg:
792
2.03k
        case ConversionSpecifier::cArg:
793
2.03k
        case ConversionSpecifier::sArg:
794
2.03k
        case ConversionSpecifier::ScanListArg:
795
2.03k
        case ConversionSpecifier::ZArg:
796
2.03k
          return true;
797
18
        case ConversionSpecifier::FreeBSDrArg:
798
18
        case ConversionSpecifier::FreeBSDyArg:
799
18
          return Target.getTriple().isOSFreeBSD() || 
Target.getTriple().isPS4()6
;
800
8
        default:
801
8
          return false;
802
0
      }
803
804
118
    case LengthModifier::AsLongDouble:
805
118
      switch (CS.getKind()) {
806
57
        case ConversionSpecifier::aArg:
807
57
        case ConversionSpecifier::AArg:
808
57
        case ConversionSpecifier::fArg:
809
57
        case ConversionSpecifier::FArg:
810
57
        case ConversionSpecifier::eArg:
811
57
        case ConversionSpecifier::EArg:
812
57
        case ConversionSpecifier::gArg:
813
57
        case ConversionSpecifier::GArg:
814
57
          return true;
815
        // GNU libc extension.
816
43
        case ConversionSpecifier::dArg:
817
43
        case ConversionSpecifier::iArg:
818
43
        case ConversionSpecifier::oArg:
819
43
        case ConversionSpecifier::uArg:
820
43
        case ConversionSpecifier::xArg:
821
43
        case ConversionSpecifier::XArg:
822
43
          return !Target.getTriple().isOSDarwin() &&
823
37
                 !Target.getTriple().isOSWindows();
824
18
        default:
825
18
          return false;
826
0
      }
827
828
6
    case LengthModifier::AsAllocate:
829
6
      switch (CS.getKind()) {
830
6
        case ConversionSpecifier::sArg:
831
6
        case ConversionSpecifier::SArg:
832
6
        case ConversionSpecifier::ScanListArg:
833
6
          return true;
834
0
        default:
835
0
          return false;
836
0
      }
837
838
23
    case LengthModifier::AsMAllocate:
839
23
      switch (CS.getKind()) {
840
21
        case ConversionSpecifier::cArg:
841
21
        case ConversionSpecifier::CArg:
842
21
        case ConversionSpecifier::sArg:
843
21
        case ConversionSpecifier::SArg:
844
21
        case ConversionSpecifier::ScanListArg:
845
21
          return true;
846
2
        default:
847
2
          return false;
848
0
      }
849
11
    case LengthModifier::AsInt32:
850
11
    case LengthModifier::AsInt3264:
851
11
    case LengthModifier::AsInt64:
852
11
      switch (CS.getKind()) {
853
11
        case ConversionSpecifier::dArg:
854
11
        case ConversionSpecifier::iArg:
855
11
        case ConversionSpecifier::oArg:
856
11
        case ConversionSpecifier::uArg:
857
11
        case ConversionSpecifier::xArg:
858
11
        case ConversionSpecifier::XArg:
859
11
          return Target.getTriple().isOSMSVCRT();
860
0
        default:
861
0
          return false;
862
0
      }
863
18
    case LengthModifier::AsWide:
864
18
      switch (CS.getKind()) {
865
18
        case ConversionSpecifier::cArg:
866
18
        case ConversionSpecifier::CArg:
867
18
        case ConversionSpecifier::sArg:
868
18
        case ConversionSpecifier::SArg:
869
18
        case ConversionSpecifier::ZArg:
870
18
          return Target.getTriple().isOSMSVCRT();
871
0
        default:
872
0
          return false;
873
0
      }
874
0
  }
875
0
  llvm_unreachable("Invalid LengthModifier Kind!");
876
0
}
877
878
26.0k
bool FormatSpecifier::hasStandardLengthModifier() const {
879
26.0k
  switch (LM.getKind()) {
880
25.8k
    case LengthModifier::None:
881
25.8k
    case LengthModifier::AsChar:
882
25.8k
    case LengthModifier::AsShort:
883
25.8k
    case LengthModifier::AsLong:
884
25.8k
    case LengthModifier::AsLongLong:
885
25.8k
    case LengthModifier::AsIntMax:
886
25.8k
    case LengthModifier::AsSizeT:
887
25.8k
    case LengthModifier::AsPtrDiff:
888
25.8k
    case LengthModifier::AsLongDouble:
889
25.8k
      return true;
890
114
    case LengthModifier::AsAllocate:
891
114
    case LengthModifier::AsMAllocate:
892
114
    case LengthModifier::AsQuad:
893
114
    case LengthModifier::AsInt32:
894
114
    case LengthModifier::AsInt3264:
895
114
    case LengthModifier::AsInt64:
896
114
    case LengthModifier::AsWide:
897
114
    case LengthModifier::AsShortLong: // ???
898
114
      return false;
899
0
  }
900
0
  llvm_unreachable("Invalid LengthModifier Kind!");
901
0
}
902
903
bool FormatSpecifier::hasStandardConversionSpecifier(
904
26.0k
    const LangOptions &LangOpt) const {
905
26.0k
  switch (CS.getKind()) {
906
25.6k
    case ConversionSpecifier::cArg:
907
25.6k
    case ConversionSpecifier::dArg:
908
25.6k
    case ConversionSpecifier::iArg:
909
25.6k
    case ConversionSpecifier::oArg:
910
25.6k
    case ConversionSpecifier::uArg:
911
25.6k
    case ConversionSpecifier::xArg:
912
25.6k
    case ConversionSpecifier::XArg:
913
25.6k
    case ConversionSpecifier::fArg:
914
25.6k
    case ConversionSpecifier::FArg:
915
25.6k
    case ConversionSpecifier::eArg:
916
25.6k
    case ConversionSpecifier::EArg:
917
25.6k
    case ConversionSpecifier::gArg:
918
25.6k
    case ConversionSpecifier::GArg:
919
25.6k
    case ConversionSpecifier::aArg:
920
25.6k
    case ConversionSpecifier::AArg:
921
25.6k
    case ConversionSpecifier::sArg:
922
25.6k
    case ConversionSpecifier::pArg:
923
25.6k
    case ConversionSpecifier::nArg:
924
25.6k
    case ConversionSpecifier::ObjCObjArg:
925
25.6k
    case ConversionSpecifier::ScanListArg:
926
25.6k
    case ConversionSpecifier::PercentArg:
927
25.6k
    case ConversionSpecifier::PArg:
928
25.6k
      return true;
929
286
    case ConversionSpecifier::CArg:
930
286
    case ConversionSpecifier::SArg:
931
286
      return LangOpt.ObjC;
932
102
    case ConversionSpecifier::InvalidSpecifier:
933
102
    case ConversionSpecifier::FreeBSDbArg:
934
102
    case ConversionSpecifier::FreeBSDDArg:
935
102
    case ConversionSpecifier::FreeBSDrArg:
936
102
    case ConversionSpecifier::FreeBSDyArg:
937
102
    case ConversionSpecifier::PrintErrno:
938
102
    case ConversionSpecifier::DArg:
939
102
    case ConversionSpecifier::OArg:
940
102
    case ConversionSpecifier::UArg:
941
102
    case ConversionSpecifier::ZArg:
942
102
      return false;
943
0
  }
944
0
  llvm_unreachable("Invalid ConversionSpecifier Kind!");
945
0
}
946
947
25.8k
bool FormatSpecifier::hasStandardLengthConversionCombination() const {
948
25.8k
  if (LM.getKind() == LengthModifier::AsLongDouble) {
949
72
    switch(CS.getKind()) {
950
33
        case ConversionSpecifier::dArg:
951
33
        case ConversionSpecifier::iArg:
952
33
        case ConversionSpecifier::oArg:
953
33
        case ConversionSpecifier::uArg:
954
33
        case ConversionSpecifier::xArg:
955
33
        case ConversionSpecifier::XArg:
956
33
          return false;
957
39
        default:
958
39
          return true;
959
25.8k
    }
960
25.8k
  }
961
25.8k
  return true;
962
25.8k
}
963
964
203
Optional<LengthModifier> FormatSpecifier::getCorrectedLengthModifier() const {
965
203
  if (CS.isAnyIntArg() || 
CS.getKind() == ConversionSpecifier::nArg109
) {
966
108
    if (LM.getKind() == LengthModifier::AsLongDouble ||
967
65
        LM.getKind() == LengthModifier::AsQuad) {
968
65
      LengthModifier FixedLM(LM);
969
65
      FixedLM.setKind(LengthModifier::AsLongLong);
970
65
      return FixedLM;
971
65
    }
972
138
  }
973
974
138
  return None;
975
138
}
976
977
bool FormatSpecifier::namedTypeToLengthModifier(QualType QT,
978
57
                                                LengthModifier &LM) {
979
57
  assert(isa<TypedefType>(QT) && "Expected a TypedefType");
980
57
  const TypedefNameDecl *Typedef = cast<TypedefType>(QT)->getDecl();
981
982
65
  for (;;) {
983
65
    const IdentifierInfo *Identifier = Typedef->getIdentifier();
984
65
    if (Identifier->getName() == "size_t") {
985
6
      LM.setKind(LengthModifier::AsSizeT);
986
6
      return true;
987
59
    } else if (Identifier->getName() == "ssize_t") {
988
      // Not C99, but common in Unix.
989
2
      LM.setKind(LengthModifier::AsSizeT);
990
2
      return true;
991
57
    } else if (Identifier->getName() == "intmax_t") {
992
4
      LM.setKind(LengthModifier::AsIntMax);
993
4
      return true;
994
53
    } else if (Identifier->getName() == "uintmax_t") {
995
4
      LM.setKind(LengthModifier::AsIntMax);
996
4
      return true;
997
49
    } else if (Identifier->getName() == "ptrdiff_t") {
998
4
      LM.setKind(LengthModifier::AsPtrDiff);
999
4
      return true;
1000
4
    }
1001
1002
45
    QualType T = Typedef->getUnderlyingType();
1003
45
    if (!isa<TypedefType>(T))
1004
37
      break;
1005
1006
8
    Typedef = cast<TypedefType>(T)->getDecl();
1007
8
  }
1008
37
  return false;
1009
57
}