1 //===- unittest/Format/FormatTestUtils.h - Formatting unit tests ----------===// 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 // This file defines utility functions for Clang-Format related tests. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_CLANG_UNITTESTS_FORMAT_FORMATTESTUTILS_H 15 #define LLVM_CLANG_UNITTESTS_FORMAT_FORMATTESTUTILS_H 16 17 #include "llvm/ADT/StringRef.h" 18 19 namespace clang { 20 namespace format { 21 namespace test { 22 messUp(llvm::StringRef Code)23inline std::string messUp(llvm::StringRef Code) { 24 std::string MessedUp(Code.str()); 25 bool InComment = false; 26 bool InPreprocessorDirective = false; 27 bool JustReplacedNewline = false; 28 for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) { 29 if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') { 30 if (JustReplacedNewline) 31 MessedUp[i - 1] = '\n'; 32 InComment = true; 33 } else if (MessedUp[i] == '#' && (JustReplacedNewline || i == 0)) { 34 if (i != 0) 35 MessedUp[i - 1] = '\n'; 36 InPreprocessorDirective = true; 37 } else if (MessedUp[i] == '\\' && MessedUp[i + 1] == '\n') { 38 MessedUp[i] = ' '; 39 MessedUp[i + 1] = ' '; 40 } else if (MessedUp[i] == '\n') { 41 if (InComment) { 42 InComment = false; 43 } else if (InPreprocessorDirective) { 44 InPreprocessorDirective = false; 45 } else { 46 JustReplacedNewline = true; 47 MessedUp[i] = ' '; 48 } 49 } else if (MessedUp[i] != ' ') { 50 JustReplacedNewline = false; 51 } 52 } 53 std::string WithoutWhitespace; 54 if (MessedUp[0] != ' ') 55 WithoutWhitespace.push_back(MessedUp[0]); 56 for (unsigned i = 1, e = MessedUp.size(); i != e; ++i) { 57 if (MessedUp[i] != ' ' || MessedUp[i - 1] != ' ') 58 WithoutWhitespace.push_back(MessedUp[i]); 59 } 60 return WithoutWhitespace; 61 } 62 63 } // end namespace test 64 } // end namespace format 65 } // end namespace clang 66 67 #endif 68