blob: bd340e5b0e60e8f5b2c689474f8146192dc97c3a [file] [log] [blame]
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +00001//===- 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
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000014#ifndef LLVM_CLANG_UNITTESTS_FORMAT_FORMATTESTUTILS_H
15#define LLVM_CLANG_UNITTESTS_FORMAT_FORMATTESTUTILS_H
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000016
17#include "llvm/ADT/StringRef.h"
18
19namespace clang {
20namespace format {
21namespace test {
22
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
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000067#endif