1a1b5780eSSam McCall //===--- RawStringLiteral.cpp ------------------------------------*- C++-*-===//
2a1b5780eSSam McCall //
3a1b5780eSSam McCall // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4a1b5780eSSam McCall // See https://llvm.org/LICENSE.txt for license information.
5a1b5780eSSam McCall // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6a1b5780eSSam McCall //
7a1b5780eSSam McCall //===----------------------------------------------------------------------===//
8915f9785SSam McCall #include "ParsedAST.h"
9a1b5780eSSam McCall #include "refactor/Tweak.h"
10*2f3d4f6cSrobozati #include "clang/AST/ASTContext.h"
11a1b5780eSSam McCall #include "clang/AST/Stmt.h"
12a1b5780eSSam McCall #include "clang/Basic/LangOptions.h"
13a1b5780eSSam McCall #include "clang/Basic/SourceLocation.h"
14a1b5780eSSam McCall #include "clang/Basic/SourceManager.h"
15a1b5780eSSam McCall #include "clang/Tooling/Core/Replacement.h"
16a1b5780eSSam McCall #include "llvm/ADT/StringRef.h"
17a1b5780eSSam McCall #include "llvm/Support/Casting.h"
18a1b5780eSSam McCall #include "llvm/Support/Error.h"
19a1b5780eSSam McCall
20a1b5780eSSam McCall namespace clang {
21a1b5780eSSam McCall namespace clangd {
22a1b5780eSSam McCall namespace {
23a1b5780eSSam McCall /// Converts a string literal to a raw string.
24a1b5780eSSam McCall /// Before:
25a1b5780eSSam McCall /// printf("\"a\"\nb");
26a1b5780eSSam McCall /// ^^^^^^^^^
27a1b5780eSSam McCall /// After:
28a1b5780eSSam McCall /// printf(R"("a"
29a1b5780eSSam McCall /// b)");
30a1b5780eSSam McCall class RawStringLiteral : public Tweak {
31a1b5780eSSam McCall public:
3295a932fbSKazu Hirata const char *id() const final;
33a1b5780eSSam McCall
34a1b5780eSSam McCall bool prepare(const Selection &Inputs) override;
35395fde75SSam McCall Expected<Effect> apply(const Selection &Inputs) override;
title() const36395fde75SSam McCall std::string title() const override { return "Convert to raw string"; }
kind() const3717747d2eSSam McCall llvm::StringLiteral kind() const override {
3817747d2eSSam McCall return CodeAction::REFACTOR_KIND;
3917747d2eSSam McCall }
40a1b5780eSSam McCall
41a1b5780eSSam McCall private:
42a1b5780eSSam McCall const clang::StringLiteral *Str = nullptr;
43a1b5780eSSam McCall };
44a1b5780eSSam McCall
REGISTER_TWEAK(RawStringLiteral)45a1b5780eSSam McCall REGISTER_TWEAK(RawStringLiteral)
46a1b5780eSSam McCall
47*2f3d4f6cSrobozati static bool isFeatureAvailable(const ASTContext &Context) {
48*2f3d4f6cSrobozati // Raw strings are available only for C++11 or later versions, and they are
49*2f3d4f6cSrobozati // not available for C.
50*2f3d4f6cSrobozati return Context.getLangOpts().CPlusPlus11;
51*2f3d4f6cSrobozati }
52*2f3d4f6cSrobozati
isNormalString(const StringLiteral & Str,SourceLocation Cursor,SourceManager & SM)53a1b5780eSSam McCall static bool isNormalString(const StringLiteral &Str, SourceLocation Cursor,
54a1b5780eSSam McCall SourceManager &SM) {
55a1b5780eSSam McCall // All chunks must be normal ASCII strings, not u8"..." etc.
56a9a60f20SCorentin Jabot if (!Str.isOrdinary())
57a1b5780eSSam McCall return false;
58a1b5780eSSam McCall SourceLocation LastTokenBeforeCursor;
59a1b5780eSSam McCall for (auto I = Str.tokloc_begin(), E = Str.tokloc_end(); I != E; ++I) {
60a1b5780eSSam McCall if (I->isMacroID()) // No tokens in the string may be macro expansions.
61a1b5780eSSam McCall return false;
62a1b5780eSSam McCall if (SM.isBeforeInTranslationUnit(*I, Cursor) || *I == Cursor)
63a1b5780eSSam McCall LastTokenBeforeCursor = *I;
64a1b5780eSSam McCall }
65a1b5780eSSam McCall // Token we care about must be a normal "string": not raw, u8, etc.
66a1b5780eSSam McCall const char* Data = SM.getCharacterData(LastTokenBeforeCursor);
67a1b5780eSSam McCall return Data && *Data == '"';
68a1b5780eSSam McCall }
69a1b5780eSSam McCall
needsRaw(llvm::StringRef Content)70a1b5780eSSam McCall static bool needsRaw(llvm::StringRef Content) {
71a1b5780eSSam McCall return Content.find_first_of("\"\n\t") != StringRef::npos;
72a1b5780eSSam McCall }
73a1b5780eSSam McCall
canBeRaw(llvm::StringRef Content)74a1b5780eSSam McCall static bool canBeRaw(llvm::StringRef Content) {
75a1b5780eSSam McCall for (char C : Content)
76a1b5780eSSam McCall if (!llvm::isPrint(C) && C != '\n' && C != '\t')
77a1b5780eSSam McCall return false;
78a1b5780eSSam McCall return !Content.contains(")\"");
79a1b5780eSSam McCall }
80a1b5780eSSam McCall
prepare(const Selection & Inputs)81a1b5780eSSam McCall bool RawStringLiteral::prepare(const Selection &Inputs) {
82*2f3d4f6cSrobozati if (!isFeatureAvailable(Inputs.AST->getASTContext())) {
83*2f3d4f6cSrobozati return false;
84*2f3d4f6cSrobozati }
85a1b5780eSSam McCall const SelectionTree::Node *N = Inputs.ASTSelection.commonAncestor();
86a1b5780eSSam McCall if (!N)
87a1b5780eSSam McCall return false;
88a1b5780eSSam McCall Str = dyn_cast_or_null<StringLiteral>(N->ASTNode.get<Stmt>());
89a1b5780eSSam McCall return Str &&
907dc388bdSSam McCall isNormalString(*Str, Inputs.Cursor, Inputs.AST->getSourceManager()) &&
91a1b5780eSSam McCall needsRaw(Str->getBytes()) && canBeRaw(Str->getBytes());
92a1b5780eSSam McCall }
93a1b5780eSSam McCall
apply(const Selection & Inputs)94395fde75SSam McCall Expected<Tweak::Effect> RawStringLiteral::apply(const Selection &Inputs) {
957dc388bdSSam McCall auto &SM = Inputs.AST->getSourceManager();
965b270932SKadir Cetinkaya auto Reps = tooling::Replacements(
97e1b07aacSAlex Lorenz tooling::Replacement(SM, Str, ("R\"(" + Str->getBytes() + ")\"").str(),
987dc388bdSSam McCall Inputs.AST->getLangOpts()));
995b270932SKadir Cetinkaya return Effect::mainFileEdit(SM, std::move(Reps));
100a1b5780eSSam McCall }
101a1b5780eSSam McCall
102a1b5780eSSam McCall } // namespace
103a1b5780eSSam McCall } // namespace clangd
104a1b5780eSSam McCall } // namespace clang
105