blob: d82d84ebedeffa495cf917fe0eade0c6b17f2aa7 [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;
Krasimir Georgievad47c902017-08-30 14:34:57 +000033 } else if (MessedUp[i] == '#' &&
34 (JustReplacedNewline || i == 0 || MessedUp[i - 1] == '\n')) {
Alexander Kornienkoa48a12c2013-12-03 10:50:16 +000035 if (i != 0)
36 MessedUp[i - 1] = '\n';
37 InPreprocessorDirective = true;
38 } else if (MessedUp[i] == '\\' && MessedUp[i + 1] == '\n') {
39 MessedUp[i] = ' ';
40 MessedUp[i + 1] = ' ';
41 } else if (MessedUp[i] == '\n') {
42 if (InComment) {
43 InComment = false;
44 } else if (InPreprocessorDirective) {
45 InPreprocessorDirective = false;
46 } else {
47 JustReplacedNewline = true;
48 MessedUp[i] = ' ';
49 }
50 } else if (MessedUp[i] != ' ') {
51 JustReplacedNewline = false;
52 }
53 }
54 std::string WithoutWhitespace;
55 if (MessedUp[0] != ' ')
56 WithoutWhitespace.push_back(MessedUp[0]);
57 for (unsigned i = 1, e = MessedUp.size(); i != e; ++i) {
58 if (MessedUp[i] != ' ' || MessedUp[i - 1] != ' ')
59 WithoutWhitespace.push_back(MessedUp[i]);
60 }
61 return WithoutWhitespace;
62}
63
64} // end namespace test
65} // end namespace format
66} // end namespace clang
67
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000068#endif