clang-tools 10.0.0

RawStringLiteral.cpp
Go to the documentation of this file.
1 //===--- RawStringLiteral.cpp ------------------------------------*- 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 #include "Logger.h"
9 #include "ParsedAST.h"
10 #include "SourceCode.h"
11 #include "refactor/Tweak.h"
12 #include "clang/AST/ASTContext.h"
13 #include "clang/AST/RecursiveASTVisitor.h"
14 #include "clang/AST/Stmt.h"
15 #include "clang/Basic/LangOptions.h"
16 #include "clang/Basic/SourceLocation.h"
17 #include "clang/Basic/SourceManager.h"
18 #include "clang/Lex/Lexer.h"
19 #include "clang/Tooling/Core/Replacement.h"
20 #include "llvm/ADT/None.h"
21 #include "llvm/ADT/Optional.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/ADT/iterator_range.h"
24 #include "llvm/Support/Casting.h"
25 #include "llvm/Support/Error.h"
26
27 namespace clang {
28 namespace clangd {
29 namespace {
30 /// Converts a string literal to a raw string.
31 /// Before:
32 /// printf("\"a\"\nb");
33 /// ^^^^^^^^^
34 /// After:
35 /// printf(R"("a"
36 /// b)");
37 class RawStringLiteral : public Tweak {
38 public:
39 const char *id() const override final;
40
41 bool prepare(const Selection &Inputs) override;
42 Expected<Effect> apply(const Selection &Inputs) override;
43 std::string title() const override { return "Convert to raw string"; }
44 Intent intent() const override { return Refactor; }
45
46 private:
47 const clang::StringLiteral *Str = nullptr;
48 };
49
50 REGISTER_TWEAK(RawStringLiteral)
51
52 static bool isNormalString(const StringLiteral &Str, SourceLocation Cursor,
53 SourceManager &SM) {
54 // All chunks must be normal ASCII strings, not u8"..." etc.
55 if (!Str.isAscii())
56 return false;
57 SourceLocation LastTokenBeforeCursor;
58 for (auto I = Str.tokloc_begin(), E = Str.tokloc_end(); I != E; ++I) {
59 if (I->isMacroID()) // No tokens in the string may be macro expansions.
60 return false;
61 if (SM.isBeforeInTranslationUnit(*I, Cursor) || *I == Cursor)
62 LastTokenBeforeCursor = *I;
63 }
64 // Token we care about must be a normal "string": not raw, u8, etc.
65 const char* Data = SM.getCharacterData(LastTokenBeforeCursor);
66 return Data && *Data == '"';
67 }
68
69 static bool needsRaw(llvm::StringRef Content) {
70 return Content.find_first_of("\"\n\t") != StringRef::npos;
71 }
72
73 static bool canBeRaw(llvm::StringRef Content) {
74 for (char C : Content)
75 if (!llvm::isPrint(C) && C != '\n' && C != '\t')
76 return false;
77 return !Content.contains(")\"");
78 }
79
80 bool RawStringLiteral::prepare(const Selection &Inputs) {
81 const SelectionTree::Node *N = Inputs.ASTSelection.commonAncestor();
82 if (!N)
83 return false;
84 Str = dyn_cast_or_null<StringLiteral>(N->ASTNode.get<Stmt>());
85 return Str &&
86 isNormalString(*Str, Inputs.Cursor, Inputs.AST->getSourceManager()) &&
87 needsRaw(Str->getBytes()) && canBeRaw(Str->getBytes());
88 }
89
90 Expected<Tweak::Effect> RawStringLiteral::apply(const Selection &Inputs) {
91 auto &SM = Inputs.AST->getSourceManager();
92 auto Reps = tooling::Replacements(
93 tooling::Replacement(SM, Str, ("R\"(" + Str->getBytes() + ")\"").str(),
94 Inputs.AST->getLangOpts()));
95 return Effect::mainFileEdit(SM, std::move(Reps));
96 }
97
98 } // namespace
99 } // namespace clangd
100 } // namespace clang
SourceCode.h
REGISTER_TWEAK
#define REGISTER_TWEAK(Subclass)
Definition: Tweak.h:129
Tweak.h
Logger.h
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
ParsedAST.h
Expected
std::vector< const char * > Expected
Definition: PrintASTTests.cpp:27
E
const Expr * E
Definition: AvoidBindCheck.cpp:82