1 //===--- TestSupport.h - Clang-based refactoring tool -----------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 /// 10 /// \file 11 /// Declares datatypes and routines that are used by test-specific code 12 /// in clang-refactor. 13 /// 14 //===----------------------------------------------------------------------===// 15 16 #ifndef LLVM_CLANG_TOOLS_CLANG_REFACTOR_TEST_SUPPORT_H 17 #define LLVM_CLANG_TOOLS_CLANG_REFACTOR_TEST_SUPPORT_H 18 19 #include "ToolRefactoringResultConsumer.h" 20 #include "clang/Basic/LLVM.h" 21 #include "clang/Basic/SourceLocation.h" 22 #include "llvm/ADT/Optional.h" 23 #include "llvm/ADT/SmallVector.h" 24 #include "llvm/Support/Error.h" 25 #include <map> 26 #include <string> 27 28 namespace clang { 29 30 class SourceManager; 31 32 namespace refactor { 33 34 /// A source selection range that's specified in a test file using an inline 35 /// command in the comment. These commands can take the following forms: 36 /// 37 /// - /*range=*/ will create an empty selection range in the default group 38 /// right after the comment. 39 /// - /*range a=*/ will create an empty selection range in the 'a' group right 40 /// after the comment. 41 /// - /*range = +1*/ will create an empty selection range at a location that's 42 /// right after the comment with one offset to the column. 43 /// - /*range= -> +2:3*/ will create a selection range that starts at the 44 /// location right after the comment, and ends at column 3 of the 2nd line 45 /// after the line of the starting location. 46 /// 47 /// Clang-refactor will expected all ranges in one test group to produce 48 /// identical results. 49 struct TestSelectionRange { 50 unsigned Begin, End; 51 }; 52 53 /// A set of test selection ranges specified in one file. 54 struct TestSelectionRangesInFile { 55 std::string Filename; 56 struct RangeGroup { 57 std::string Name; 58 SmallVector<TestSelectionRange, 8> Ranges; 59 }; 60 std::vector<RangeGroup> GroupedRanges; 61 62 bool foreachRange(const SourceManager &SM, 63 llvm::function_ref<void(SourceRange)> Callback) const; 64 65 std::unique_ptr<ClangRefactorToolConsumerInterface> createConsumer() const; 66 67 void dump(llvm::raw_ostream &OS) const; 68 }; 69 70 /// Extracts the grouped selection ranges from the file that's specified in 71 /// the -selection=test:<filename> option. 72 /// 73 /// The grouped ranges are specified in comments using the following syntax: 74 /// "range" [ group-name ] "=" [ "+" starting-column-offset ] [ "->" 75 /// "+" ending-line-offset ":" 76 /// ending-column-position ] 77 /// 78 /// The selection range is then computed from this command by taking the ending 79 /// location of the comment, and adding 'starting-column-offset' to the column 80 /// for that location. That location in turns becomes the whole selection range, 81 /// unless 'ending-line-offset' and 'ending-column-position' are specified. If 82 /// they are specified, then the ending location of the selection range is 83 /// the starting location's line + 'ending-line-offset' and the 84 /// 'ending-column-position' column. 85 /// 86 /// All selection ranges in one group are expected to produce the same 87 /// refactoring result. 88 /// 89 /// When testing, zero is returned from clang-refactor even when a group 90 /// produces an initiation error, which is different from normal invocation 91 /// that returns a non-zero value. This is done on purpose, to ensure that group 92 /// consistency checks can return non-zero, but still print the output of 93 /// the group. So even if a test matches the output of group, it will still fail 94 /// because clang-refactor should return zero on exit when the group results are 95 /// consistent. 96 /// 97 /// \returns None on failure (errors are emitted to stderr), or a set of 98 /// grouped source ranges in the given file otherwise. 99 Optional<TestSelectionRangesInFile> findTestSelectionRanges(StringRef Filename); 100 101 } // end namespace refactor 102 } // end namespace clang 103 104 #endif // LLVM_CLANG_TOOLS_CLANG_REFACTOR_TEST_SUPPORT_H 105