[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / clang-tools-extra / clang-tidy / objc / NSDateFormatterCheck.cpp
blobf8a4d6e95df0a48f6bfd3d62ab9ecc7dd8fa1e6a
1 //===--- NSDateFormatterCheck.cpp - clang-tidy ----------------------------===//
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 //===----------------------------------------------------------------------===//
9 #include "NSDateFormatterCheck.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/ASTMatchers/ASTMatchFinder.h"
12 #include "clang/ASTMatchers/ASTMatchers.h"
14 using namespace clang::ast_matchers;
16 namespace clang {
17 namespace tidy {
18 namespace objc {
20 void NSDateFormatterCheck::registerMatchers(MatchFinder *Finder) {
21 // Adding matchers.
23 Finder->addMatcher(
24 objcMessageExpr(hasSelector("setDateFormat:"),
25 hasReceiverType(asString("NSDateFormatter *")),
26 hasArgument(0, ignoringImpCasts(
27 objcStringLiteral().bind("str_lit")))),
28 this);
31 static char ValidDatePatternChars[] = {
32 'G', 'y', 'Y', 'u', 'U', 'r', 'Q', 'q', 'M', 'L', 'I', 'w', 'W', 'd',
33 'D', 'F', 'g', 'E', 'e', 'c', 'a', 'b', 'B', 'h', 'H', 'K', 'k', 'j',
34 'J', 'C', 'm', 's', 'S', 'A', 'z', 'Z', 'O', 'v', 'V', 'X', 'x'};
36 // Checks if the string pattern used as a date format specifier is valid.
37 // A string pattern is valid if all the letters(a-z, A-Z) in it belong to the
38 // set of reserved characters. See:
39 // https://www.unicode.org/reports/tr35/tr35.html#Invalid_Patterns
40 bool isValidDatePattern(StringRef Pattern) {
41 return llvm::all_of(Pattern, [](const auto &PatternChar) {
42 return !isalpha(PatternChar) ||
43 llvm::is_contained(ValidDatePatternChars, PatternChar);
44 });
47 // Checks if the string pattern used as a date format specifier contains
48 // any incorrect pattern and reports it as a warning.
49 // See: http://www.unicode.org/reports/tr35/tr35-dates.html#Date_Format_Patterns
50 void NSDateFormatterCheck::check(const MatchFinder::MatchResult &Result) {
51 // Callback implementation.
52 const auto *StrExpr = Result.Nodes.getNodeAs<ObjCStringLiteral>("str_lit");
53 const StringLiteral *SL = cast<ObjCStringLiteral>(StrExpr)->getString();
54 StringRef SR = SL->getString();
56 if (!isValidDatePattern(SR)) {
57 diag(StrExpr->getExprLoc(), "invalid date format specifier");
60 if (SR.contains('y') && SR.contains('w') && !SR.contains('Y')) {
61 diag(StrExpr->getExprLoc(),
62 "use of calendar year (y) with week of the year (w); "
63 "did you mean to use week-year (Y) instead?");
65 if (SR.contains('F')) {
66 if (!(SR.contains('e') || SR.contains('E'))) {
67 diag(StrExpr->getExprLoc(),
68 "day of week in month (F) used without day of the week (e or E); "
69 "did you forget e (or E) in the format string?");
71 if (!SR.contains('M')) {
72 diag(StrExpr->getExprLoc(),
73 "day of week in month (F) used without the month (M); "
74 "did you forget M in the format string?");
77 if (SR.contains('W') && !SR.contains('M')) {
78 diag(StrExpr->getExprLoc(), "Week of Month (W) used without the month (M); "
79 "did you forget M in the format string?");
81 if (SR.contains('Y') && SR.contains('Q') && !SR.contains('y')) {
82 diag(StrExpr->getExprLoc(),
83 "use of week year (Y) with quarter number (Q); "
84 "did you mean to use calendar year (y) instead?");
86 if (SR.contains('Y') && SR.contains('M') && !SR.contains('y')) {
87 diag(StrExpr->getExprLoc(),
88 "use of week year (Y) with month (M); "
89 "did you mean to use calendar year (y) instead?");
91 if (SR.contains('Y') && SR.contains('D') && !SR.contains('y')) {
92 diag(StrExpr->getExprLoc(),
93 "use of week year (Y) with day of the year (D); "
94 "did you mean to use calendar year (y) instead?");
96 if (SR.contains('Y') && SR.contains('W') && !SR.contains('y')) {
97 diag(StrExpr->getExprLoc(),
98 "use of week year (Y) with week of the month (W); "
99 "did you mean to use calendar year (y) instead?");
101 if (SR.contains('Y') && SR.contains('F') && !SR.contains('y')) {
102 diag(StrExpr->getExprLoc(),
103 "use of week year (Y) with day of the week in month (F); "
104 "did you mean to use calendar year (y) instead?");
108 } // namespace objc
109 } // namespace tidy
110 } // namespace clang