1 //===- unittest/Format/FormatTestUtils.h - Formatting unit tests ----------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines utility functions for Clang-Format related tests.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_UNITTESTS_FORMAT_FORMATTESTUTILS_H
14 #define LLVM_CLANG_UNITTESTS_FORMAT_FORMATTESTUTILS_H
16 #include "clang/Format/Format.h"
17 #include "llvm/ADT/StringRef.h"
23 inline FormatStyle
getGoogleStyle() {
24 return getGoogleStyle(FormatStyle::LK_Cpp
);
27 // When HandleHash is false, preprocessor directives starting with hash will not
28 // be on separate lines. This is needed because Verilog uses hash for other
30 inline std::string
messUp(StringRef Code
, bool HandleHash
= true) {
31 std::string
MessedUp(Code
.str());
32 bool InComment
= false;
33 bool InPreprocessorDirective
= false;
34 bool JustReplacedNewline
= false;
35 for (unsigned i
= 0, e
= MessedUp
.size() - 1; i
!= e
; ++i
) {
36 if (MessedUp
[i
] == '/' && MessedUp
[i
+ 1] == '/') {
37 if (JustReplacedNewline
)
38 MessedUp
[i
- 1] = '\n';
40 } else if (HandleHash
&& MessedUp
[i
] == '#' &&
41 (JustReplacedNewline
|| i
== 0 || MessedUp
[i
- 1] == '\n')) {
43 MessedUp
[i
- 1] = '\n';
44 InPreprocessorDirective
= true;
45 } else if (MessedUp
[i
] == '\\' && MessedUp
[i
+ 1] == '\n') {
47 MessedUp
[i
+ 1] = ' ';
48 } else if (MessedUp
[i
] == '\n') {
51 } else if (InPreprocessorDirective
) {
52 InPreprocessorDirective
= false;
54 JustReplacedNewline
= true;
57 } else if (MessedUp
[i
] != ' ') {
58 JustReplacedNewline
= false;
61 std::string WithoutWhitespace
;
62 if (MessedUp
[0] != ' ')
63 WithoutWhitespace
.push_back(MessedUp
[0]);
64 for (unsigned i
= 1, e
= MessedUp
.size(); i
!= e
; ++i
)
65 if (MessedUp
[i
] != ' ' || MessedUp
[i
- 1] != ' ')
66 WithoutWhitespace
.push_back(MessedUp
[i
]);
67 return WithoutWhitespace
;
70 } // end namespace test
71 } // end namespace format
72 } // end namespace clang