FormatTestUtils.h 2.1 KB

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