Coverage Report

Created: 2020-02-18 08:44

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