aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/clang16/tools/extra/clang-tidy/objc/NSDateFormatterCheck.cpp
blob: fe18958ded1fd212b248e9ffd2c4e5ac5f1b1f1a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//===--- NSDateFormatterCheck.cpp - clang-tidy ----------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "NSDateFormatterCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"

using namespace clang::ast_matchers;

namespace clang::tidy::objc {

void NSDateFormatterCheck::registerMatchers(MatchFinder *Finder) {
  // Adding matchers.

  Finder->addMatcher(
      objcMessageExpr(hasSelector("setDateFormat:"),
                      hasReceiverType(asString("NSDateFormatter *")),
                      hasArgument(0, ignoringImpCasts(
                                         objcStringLiteral().bind("str_lit")))),
      this);
}

static char ValidDatePatternChars[] = {
    'G', 'y', 'Y', 'u', 'U', 'r', 'Q', 'q', 'M', 'L', 'I', 'w', 'W', 'd',
    'D', 'F', 'g', 'E', 'e', 'c', 'a', 'b', 'B', 'h', 'H', 'K', 'k', 'j',
    'J', 'C', 'm', 's', 'S', 'A', 'z', 'Z', 'O', 'v', 'V', 'X', 'x'};

// Checks if the string pattern used as a date format specifier is valid.
// A string pattern is valid if all the letters(a-z, A-Z) in it belong to the
// set of reserved characters. See:
// https://www.unicode.org/reports/tr35/tr35.html#Invalid_Patterns
bool isValidDatePattern(StringRef Pattern) {
  return llvm::all_of(Pattern, [](const auto &PatternChar) {
    return !isalpha(PatternChar) ||
           llvm::is_contained(ValidDatePatternChars, PatternChar);
  });
}

// Checks if the string pattern used as a date format specifier contains
// any incorrect pattern and reports it as a warning.
// See: http://www.unicode.org/reports/tr35/tr35-dates.html#Date_Format_Patterns
void NSDateFormatterCheck::check(const MatchFinder::MatchResult &Result) {
  // Callback implementation.
  const auto *StrExpr = Result.Nodes.getNodeAs<ObjCStringLiteral>("str_lit");
  const StringLiteral *SL = cast<ObjCStringLiteral>(StrExpr)->getString();
  StringRef SR = SL->getString();

  if (!isValidDatePattern(SR)) {
    diag(StrExpr->getExprLoc(), "invalid date format specifier");
  }

  if (SR.contains('y') && SR.contains('w') && !SR.contains('Y')) {
    diag(StrExpr->getExprLoc(),
         "use of calendar year (y) with week of the year (w); "
         "did you mean to use week-year (Y) instead?");
  }
  if (SR.contains('F')) {
    if (!(SR.contains('e') || SR.contains('E'))) {
      diag(StrExpr->getExprLoc(),
           "day of week in month (F) used without day of the week (e or E); "
           "did you forget e (or E) in the format string?");
    }
    if (!SR.contains('M')) {
      diag(StrExpr->getExprLoc(),
           "day of week in month (F) used without the month (M); "
           "did you forget M in the format string?");
    }
  }
  if (SR.contains('W') && !SR.contains('M')) {
    diag(StrExpr->getExprLoc(), "Week of Month (W) used without the month (M); "
                                "did you forget M in the format string?");
  }
  if (SR.contains('Y') && SR.contains('Q') && !SR.contains('y')) {
    diag(StrExpr->getExprLoc(),
         "use of week year (Y) with quarter number (Q); "
         "did you mean to use calendar year (y) instead?");
  }
  if (SR.contains('Y') && SR.contains('M') && !SR.contains('y')) {
    diag(StrExpr->getExprLoc(),
         "use of week year (Y) with month (M); "
         "did you mean to use calendar year (y) instead?");
  }
  if (SR.contains('Y') && SR.contains('D') && !SR.contains('y')) {
    diag(StrExpr->getExprLoc(),
         "use of week year (Y) with day of the year (D); "
         "did you mean to use calendar year (y) instead?");
  }
  if (SR.contains('Y') && SR.contains('W') && !SR.contains('y')) {
    diag(StrExpr->getExprLoc(),
         "use of week year (Y) with week of the month (W); "
         "did you mean to use calendar year (y) instead?");
  }
  if (SR.contains('Y') && SR.contains('F') && !SR.contains('y')) {
    diag(StrExpr->getExprLoc(),
         "use of week year (Y) with day of the week in month (F); "
         "did you mean to use calendar year (y) instead?");
  }
}

} // namespace clang::tidy::objc