clang: lib/Format/NumericLiteralCaseFixer.cpp Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

17

18#include "llvm/ADT/StringExtras.h"

19

20#include

21

24

26

27 switch (Style.Language) {

28 case FormatStyle::LK_C:

29 case FormatStyle::LK_Cpp:

30 case FormatStyle::LK_ObjC:

31 case FormatStyle::LK_CSharp:

32 case FormatStyle::LK_Java:

33 case FormatStyle::LK_JavaScript:

34 break;

35 default:

36 return false;

37 }

38

39

40 const auto &Option = Style.NumericLiteralCase;

41 const auto Leave = FormatStyle::NLCS_Leave;

42 return Option.Prefix != Leave || Option.HexDigit != Leave ||

43 Option.ExponentLetter != Leave || Option.Suffix != Leave;

44}

45

46static std::string

48 FormatStyle::NumericLiteralComponentStyle ConfigValue) {

49 switch (ConfigValue) {

50 case FormatStyle::NLCS_Upper:

51 return Component.upper();

52 case FormatStyle::NLCS_Lower:

53 return Component.lower();

54 default:

55

56 return Component.str();

57 }

58}

59

60

61

63 static constexpr std::array<StringRef, 11> SortedReservedSuffixes = {

64 "d", "h", "i", "if", "il", "min", "ms", "ns", "s", "us", "y",

65 };

66

67

68

69 assert(llvm::is_sorted(SortedReservedSuffixes) &&

70 "Must be sorted as precondition for lower_bound().");

71

72 auto entry = llvm::lower_bound(SortedReservedSuffixes, Suffix);

73 if (entry == SortedReservedSuffixes.cend())

74 return false;

75 return *entry == Suffix;

76}

77

78static std::string format(StringRef NumericLiteral, const FormatStyle &Style) {

79 const char Separator = Style.isCpp() ? '\'' : '_';

81 const bool HasBaseLetter = Info.BaseLetterPos != StringRef::npos;

82 const bool HasExponent = Info.ExponentLetterPos != StringRef::npos;

83 const bool HasSuffix = Info.SuffixPos != StringRef::npos;

84

85 std::string Formatted;

86

87 if (HasBaseLetter) {

88 Formatted +=

90 Style.NumericLiteralCase.Prefix);

91 }

92

93

95 NumericLiteral.slice(HasBaseLetter ? 1 + Info.BaseLetterPos : 0,

98 : NumericLiteral.size()),

99 Style.NumericLiteralCase.HexDigit);

100

101 if (HasExponent) {

105 : NumericLiteral.size()),

106 Style.NumericLiteralCase.ExponentLetter);

107 }

108

109 if (HasSuffix) {

110 StringRef Suffix = NumericLiteral.drop_front(Info.SuffixPos);

112

113

114

115 Formatted += Suffix.str();

116 } else {

117 Formatted += transformComponent(Suffix, Style.NumericLiteralCase.Suffix);

118 }

119 }

120

121 return Formatted;

122}

123

124std::pair<tooling::Replacements, unsigned>

126 const FormatStyle &Style) {

128 return {};

129

132

135 Lexer Lex(ID, SourceMgr.getBufferOrFake(ID), SourceMgr, LangOpts);

137

140

142

143

144

145 auto Length = Tok.getLength();

146 if (Length < 2)

147 continue;

148

149

150 auto Location = Tok.getLocation();

151 auto Text = StringRef(SourceMgr.getCharacterData(Location), Length);

152 if (Tok.is(tok::comment)) {

156 Skip = false;

157 continue;

158 }

159

160 if (Skip || Tok.isNot(tok::numeric_constant) ||

163 continue;

164 }

165

166 const auto Formatted = format(Text, Style);

167 if (Formatted != Text) {

170 }

171 }

172

174}

175

176}

177}

This file declares NumericLiteralCaseFixer that standardizes character case within numeric literals.

static CharSourceRange getCharRange(SourceRange R)

Lexer - This provides a simple interface that turns a text buffer into a stream of tokens.

bool LexFromRawLexer(Token &Result)

LexFromRawLexer - Lex a token from a designated raw lexer (one with no associated preprocessor object...

void SetCommentRetentionState(bool Mode)

SetCommentRetentionMode - Change the comment retention mode of the lexer to the specified mode.

Token - This structure provides full information about a lexed token.

bool affectsCharSourceRange(const CharSourceRange &Range)

SourceManager & getSourceManager() const

ArrayRef< CharSourceRange > getCharRanges() const

std::pair< tooling::Replacements, unsigned > process(const Environment &Env, const FormatStyle &Style)

Definition NumericLiteralCaseFixer.cpp:125

static std::string format(StringRef NumericLiteral, const FormatStyle &Style)

Definition NumericLiteralCaseFixer.cpp:78

static std::string transformComponent(StringRef Component, FormatStyle::NumericLiteralComponentStyle ConfigValue)

Definition NumericLiteralCaseFixer.cpp:47

static bool isNumericLiteralCaseFixerNeeded(const FormatStyle &Style)

Definition NumericLiteralCaseFixer.cpp:25

static bool matchesReservedSuffix(StringRef Suffix)

Test if Suffix matches a C++ literal reserved by the library.

Definition NumericLiteralCaseFixer.cpp:62

bool isClangFormatOff(StringRef Comment)

bool isClangFormatOn(StringRef Comment)

LangOptions getFormattingLangOpts(const FormatStyle &Style)

The JSON file list parser is used to communicate input to InstallAPI.

@ Result

The result type of a method or function.