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