reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
//===- unittests/Rewrite/RewriterTest.cpp - Rewriter tests ----------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "clang/Rewrite/Core/Rewriter.h"
#include "clang/Tooling/Tooling.h"
#include "gtest/gtest.h"

using namespace clang;

namespace {

struct RangeTypeTest {
  std::unique_ptr<ASTUnit> AST;
  Rewriter Rewrite;
  SourceLocation FileStart;
  CharSourceRange CRange; // covers exact char range
  CharSourceRange TRange; // extends CRange to whole tokens
  SourceRange SRange;     // different type but behaves like TRange
  SourceLocation makeLoc(int Off) { return FileStart.getLocWithOffset(Off); }
  CharSourceRange makeCharRange(int StartOff, int EndOff) {
    return CharSourceRange::getCharRange(makeLoc(StartOff), makeLoc(EndOff));
  }
  RangeTypeTest(StringRef Code, int StartOff, int EndOff) {
    AST = tooling::buildASTFromCode(Code);
    ASTContext &C = AST->getASTContext();
    Rewrite = Rewriter(C.getSourceManager(), C.getLangOpts());
    FileStart = AST->getStartOfMainFileID();
    CRange = makeCharRange(StartOff, EndOff);
    SRange = CRange.getAsRange();
    TRange = CharSourceRange::getTokenRange(SRange);
  }
};

TEST(Rewriter, GetRewrittenTextRangeTypes) {
  // Check that correct text is retrieved for each range type.  Check again
  // after a modification.  Ranges remain in terms of the original text but
  // include the new text.
  StringRef Code = "int main() { return 0; }";
  //              get char range ^~~    = "ret"
  //             get token range ^~~+++ = "return"
  //            get source range ^~~+++ = "return"
  //                  insert "x" ^
  //              get char range ^~~    = "xret"
  //             get token range ^~~+++ = "xreturn"
  //            get source range ^~~+++ = "xreturn"
  RangeTypeTest T(Code, 13, 16);
  EXPECT_EQ(T.Rewrite.getRewrittenText(T.CRange), "ret");
  EXPECT_EQ(T.Rewrite.getRewrittenText(T.TRange), "return");
  EXPECT_EQ(T.Rewrite.getRewrittenText(T.SRange), "return");
  T.Rewrite.InsertText(T.makeLoc(13), "x");
  EXPECT_EQ(T.Rewrite.getRewrittenText(T.CRange), "xret");
  EXPECT_EQ(T.Rewrite.getRewrittenText(T.TRange), "xreturn");
  EXPECT_EQ(T.Rewrite.getRewrittenText(T.SRange), "xreturn");
}

TEST(Rewriter, ReplaceTextRangeTypes) {
  // Check that correct text is replaced for each range type.  Ranges remain in
  // terms of the original text but include the new text.
  StringRef Code = "int main(int argc, char *argv[]) { return argc; }";
  //                            replace char range with "foo" ^~
  //                                                      get ^~~~~ = "foogc;"
  //                           replace token range with "bar" ^~++
  //                                                      get ^~~~~ = "bar;"
  //                            replace source range with "0" ^~++
  //                                                      get ^~~~~ = "0;"
  RangeTypeTest T(Code, 42, 44);
  T.Rewrite.ReplaceText(T.CRange, "foo");
  EXPECT_EQ(T.Rewrite.getRewrittenText(T.makeCharRange(42, 47)), "foogc;");
  T.Rewrite.ReplaceText(T.TRange, "bar");
  EXPECT_EQ(T.Rewrite.getRewrittenText(T.makeCharRange(42, 47)), "bar;");
  T.Rewrite.ReplaceText(T.SRange, "0");
  EXPECT_EQ(T.Rewrite.getRewrittenText(T.makeCharRange(42, 47)), "0;");
}

} // anonymous namespace