clang-tools 23.0.0git
MisleadingBidirectionalCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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
10
11#include "clang/Frontend/CompilerInstance.h"
12#include "clang/Lex/Preprocessor.h"
13#include "llvm/Support/ConvertUTF.h"
14
15using namespace clang;
16using namespace clang::tidy::misc;
17
18static bool containsMisleadingBidi(StringRef Buffer,
19 bool HonorLineBreaks = true) {
20 const char *CurPtr = Buffer.begin();
21
22 enum BidiChar {
23 PS = 0x2029,
24 RLO = 0x202E,
25 RLE = 0x202B,
26 LRO = 0x202D,
27 LRE = 0x202A,
28 PDF = 0x202C,
29 RLI = 0x2067,
30 LRI = 0x2066,
31 FSI = 0x2068,
32 PDI = 0x2069
33 };
34
35 SmallVector<BidiChar> BidiContexts;
36
37 // Scan each character while maintaining a stack of opened bidi context.
38 // RLO/RLE/LRO/LRE all are closed by PDF while RLI LRI and FSI are closed by
39 // PDI. New lines reset the context count. Extra PDF / PDI are ignored.
40 //
41 // Warn if we end up with an unclosed context.
42 while (CurPtr < Buffer.end()) {
43 const unsigned char C = *CurPtr;
44 if (isASCII(C)) {
45 ++CurPtr;
46 const bool IsParagrapSep =
47 (C == 0xA || C == 0xD || (0x1C <= C && C <= 0x1E) || C == 0x85);
48 const bool IsSegmentSep = (C == 0x9 || C == 0xB || C == 0x1F);
49 if (IsParagrapSep || IsSegmentSep)
50 BidiContexts.clear();
51 continue;
52 }
53 llvm::UTF32 CodePoint = 0;
54 const llvm::ConversionResult Result = llvm::convertUTF8Sequence(
55 reinterpret_cast<const llvm::UTF8 **>(&CurPtr),
56 reinterpret_cast<const llvm::UTF8 *>(Buffer.end()), &CodePoint,
57 llvm::strictConversion);
58
59 // If conversion fails, utf-8 is designed so that we can just try next char.
60 if (Result != llvm::conversionOK) {
61 ++CurPtr;
62 continue;
63 }
64
65 // Open a PDF context.
66 if (CodePoint == RLO || CodePoint == RLE || CodePoint == LRO ||
67 CodePoint == LRE) {
68 BidiContexts.push_back(PDF);
69 }
70 // Close PDF Context.
71 else if (CodePoint == PDF) {
72 if (!BidiContexts.empty() && BidiContexts.back() == PDF)
73 BidiContexts.pop_back();
74 }
75 // Open a PDI Context.
76 else if (CodePoint == RLI || CodePoint == LRI || CodePoint == FSI) {
77 BidiContexts.push_back(PDI);
78 }
79 // Close a PDI Context.
80 else if (CodePoint == PDI) {
81 auto R = llvm::find(llvm::reverse(BidiContexts), PDI);
82 if (R != BidiContexts.rend())
83 BidiContexts.resize(BidiContexts.rend() - R - 1);
84 }
85 // Line break or equivalent
86 else if (CodePoint == PS) {
87 BidiContexts.clear();
88 }
89 }
90 return !BidiContexts.empty();
91}
92
94 : public CommentHandler {
95public:
98
99 bool HandleComment(Preprocessor &PP, SourceRange Range) override {
100 // FIXME: check that we are in a /* */ comment
101 const StringRef Text =
102 Lexer::getSourceText(CharSourceRange::getCharRange(Range),
103 PP.getSourceManager(), PP.getLangOpts());
104
105 if (containsMisleadingBidi(Text, true))
106 Check.diag(
107 Range.getBegin(),
108 "comment contains misleading bidirectional Unicode characters");
109 return false;
110 }
111
112private:
114};
115
117 StringRef Name, ClangTidyContext *Context)
118 : ClangTidyCheck(Name, Context),
119 Handler(std::make_unique<MisleadingBidirectionalHandler>(*this)) {}
120
122
124 const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) {
125 PP->addCommentHandler(Handler.get());
126}
127
129 const ast_matchers::MatchFinder::MatchResult &Result) {
130 if (const auto *SL = Result.Nodes.getNodeAs<StringLiteral>("strlit")) {
131 const StringRef Literal = SL->getBytes();
132 if (containsMisleadingBidi(Literal, false))
133 diag(SL->getBeginLoc(), "string literal contains misleading "
134 "bidirectional Unicode characters");
135 }
136}
137
139 ast_matchers::MatchFinder *Finder) {
140 Finder->addMatcher(ast_matchers::stringLiteral().bind("strlit"), this);
141}
static bool containsMisleadingBidi(StringRef Buffer, bool HonorLineBreaks=true)
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
MisleadingBidirectionalCheck(StringRef Name, ClangTidyContext *Context)
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, Preprocessor *ModuleExpanderPP) override
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//