Coverage Report

Created: 2022-01-25 06:29

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