[clang] b81cc60 - [clang][NFC] Make various uses of Regex const
Thomas Preud'homme via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 19 08:15:43 PST 2019
Author: Thomas Preud'homme
Date: 2019-11-19T16:15:21Z
New Revision: b81cc6032902c810e0fde485d3092a947dc38541
URL: https://github.com/llvm/llvm-project/commit/b81cc6032902c810e0fde485d3092a947dc38541
DIFF: https://github.com/llvm/llvm-project/commit/b81cc6032902c810e0fde485d3092a947dc38541.diff
LOG: [clang][NFC] Make various uses of Regex const
The const-correctness of match() was fixed in rL372764, which allows
uses of Regex objects to be const in cases they couldn't be before. This
patch tightens up the const-ness of Regex in various such cases.
Reviewers: thopre
Reviewed By: thopre
Subscribers: cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D68155
Added:
Modified:
clang/include/clang/Tooling/Inclusions/HeaderIncludes.h
clang/lib/Format/BreakableToken.cpp
clang/lib/Format/BreakableToken.h
clang/lib/Format/NamespaceEndCommentsFixer.cpp
clang/lib/Format/UnwrappedLineParser.cpp
clang/tools/clang-refactor/TestSupport.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/Tooling/Inclusions/HeaderIncludes.h b/clang/include/clang/Tooling/Inclusions/HeaderIncludes.h
index 6e6d6d8fb024..18374ad46a9c 100644
--- a/clang/include/clang/Tooling/Inclusions/HeaderIncludes.h
+++ b/clang/include/clang/Tooling/Inclusions/HeaderIncludes.h
@@ -42,8 +42,7 @@ class IncludeCategoryManager {
std::string FileName;
// This refers to a substring in FileName.
StringRef FileStem;
- // Regex is not thread-safe.
- mutable SmallVector<llvm::Regex, 4> CategoryRegexs;
+ SmallVector<llvm::Regex, 4> CategoryRegexs;
};
/// Generates replacements for inserting or deleting #include directives in a
diff --git a/clang/lib/Format/BreakableToken.cpp b/clang/lib/Format/BreakableToken.cpp
index 09ea5473c0c1..cd0eb0b4324a 100644
--- a/clang/lib/Format/BreakableToken.cpp
+++ b/clang/lib/Format/BreakableToken.cpp
@@ -88,11 +88,11 @@ getCommentSplit(StringRef Text, unsigned ContentStartColumn,
StringRef::size_type SpaceOffset = Text.find_last_of(Blanks, MaxSplitBytes);
- static auto *const kNumberedListRegexp = new llvm::Regex("^[1-9][0-9]?\\.");
+ static const auto kNumberedListRegexp = llvm::Regex("^[1-9][0-9]?\\.");
while (SpaceOffset != StringRef::npos) {
// Do not split before a number followed by a dot: this would be interpreted
// as a numbered list, which would prevent re-flowing in subsequent passes.
- if (kNumberedListRegexp->match(Text.substr(SpaceOffset).ltrim(Blanks)))
+ if (kNumberedListRegexp.match(Text.substr(SpaceOffset).ltrim(Blanks)))
SpaceOffset = Text.find_last_of(Blanks, SpaceOffset);
// In JavaScript, some @tags can be followed by {, and machinery that parses
// these comments will fail to understand the comment if followed by a line
@@ -245,7 +245,7 @@ BreakableStringLiteral::BreakableStringLiteral(
BreakableToken::Split BreakableStringLiteral::getSplit(
unsigned LineIndex, unsigned TailOffset, unsigned ColumnLimit,
- unsigned ContentStartColumn, llvm::Regex &CommentPragmasRegex) const {
+ unsigned ContentStartColumn, const llvm::Regex &CommentPragmasRegex) const {
return getStringSplit(Line.substr(TailOffset), ContentStartColumn,
ColumnLimit - Postfix.size(), Style.TabWidth, Encoding);
}
@@ -271,7 +271,7 @@ unsigned BreakableComment::getLineCount() const { return Lines.size(); }
BreakableToken::Split
BreakableComment::getSplit(unsigned LineIndex, unsigned TailOffset,
unsigned ColumnLimit, unsigned ContentStartColumn,
- llvm::Regex &CommentPragmasRegex) const {
+ const llvm::Regex &CommentPragmasRegex) const {
// Don't break lines matching the comment pragmas regex.
if (CommentPragmasRegex.match(Content[LineIndex]))
return Split(StringRef::npos, 0);
@@ -316,9 +316,9 @@ static bool mayReflowContent(StringRef Content) {
// Numbered lists may also start with a number followed by '.'
// To avoid issues if a line starts with a number which is actually the end
// of a previous line, we only consider numbers with up to 2 digits.
- static auto *const kNumberedListRegexp = new llvm::Regex("^[1-9][0-9]?\\. ");
+ static const auto kNumberedListRegexp = llvm::Regex("^[1-9][0-9]?\\. ");
hasSpecialMeaningPrefix =
- hasSpecialMeaningPrefix || kNumberedListRegexp->match(Content);
+ hasSpecialMeaningPrefix || kNumberedListRegexp.match(Content);
// Simple heuristic for what to reflow: content should contain at least two
// characters and either the first or second character must be
@@ -458,7 +458,7 @@ BreakableBlockComment::BreakableBlockComment(
BreakableToken::Split BreakableBlockComment::getSplit(
unsigned LineIndex, unsigned TailOffset, unsigned ColumnLimit,
- unsigned ContentStartColumn, llvm::Regex &CommentPragmasRegex) const {
+ unsigned ContentStartColumn, const llvm::Regex &CommentPragmasRegex) const {
// Don't break lines matching the comment pragmas regex.
if (CommentPragmasRegex.match(Content[LineIndex]))
return Split(StringRef::npos, 0);
@@ -597,9 +597,8 @@ void BreakableBlockComment::insertBreak(unsigned LineIndex, unsigned TailOffset,
PrefixWithTrailingIndent.size());
}
-BreakableToken::Split
-BreakableBlockComment::getReflowSplit(unsigned LineIndex,
- llvm::Regex &CommentPragmasRegex) const {
+BreakableToken::Split BreakableBlockComment::getReflowSplit(
+ unsigned LineIndex, const llvm::Regex &CommentPragmasRegex) const {
if (!mayReflow(LineIndex, CommentPragmasRegex))
return Split(StringRef::npos, 0);
@@ -706,8 +705,8 @@ BreakableBlockComment::getSplitAfterLastLine(unsigned TailOffset) const {
return Split(StringRef::npos, 0);
}
-bool BreakableBlockComment::mayReflow(unsigned LineIndex,
- llvm::Regex &CommentPragmasRegex) const {
+bool BreakableBlockComment::mayReflow(
+ unsigned LineIndex, const llvm::Regex &CommentPragmasRegex) const {
// Content[LineIndex] may exclude the indent after the '*' decoration. In that
// case, we compute the start of the comment pragma manually.
StringRef IndentContent = Content[LineIndex];
@@ -845,7 +844,7 @@ void BreakableLineCommentSection::insertBreak(
}
BreakableComment::Split BreakableLineCommentSection::getReflowSplit(
- unsigned LineIndex, llvm::Regex &CommentPragmasRegex) const {
+ unsigned LineIndex, const llvm::Regex &CommentPragmasRegex) const {
if (!mayReflow(LineIndex, CommentPragmasRegex))
return Split(StringRef::npos, 0);
@@ -955,7 +954,7 @@ void BreakableLineCommentSection::updateNextToken(LineState &State) const {
}
bool BreakableLineCommentSection::mayReflow(
- unsigned LineIndex, llvm::Regex &CommentPragmasRegex) const {
+ unsigned LineIndex, const llvm::Regex &CommentPragmasRegex) const {
// Line comments have the indent as part of the prefix, so we need to
// recompute the start of the line.
StringRef IndentContent = Content[LineIndex];
diff --git a/clang/lib/Format/BreakableToken.h b/clang/lib/Format/BreakableToken.h
index 72852d59f9c4..a6691300de3b 100644
--- a/clang/lib/Format/BreakableToken.h
+++ b/clang/lib/Format/BreakableToken.h
@@ -155,7 +155,7 @@ class BreakableToken {
/// file.
virtual Split getSplit(unsigned LineIndex, unsigned TailOffset,
unsigned ColumnLimit, unsigned ContentStartColumn,
- llvm::Regex &CommentPragmasRegex) const = 0;
+ const llvm::Regex &CommentPragmasRegex) const = 0;
/// Emits the previously retrieved \p Split via \p Whitespaces.
virtual void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
@@ -190,7 +190,7 @@ class BreakableToken {
/// If the split is not contained within one token, for example when reflowing
/// line comments, returns (0, <length>).
virtual Split getReflowSplit(unsigned LineIndex,
- llvm::Regex &CommentPragmasRegex) const {
+ const llvm::Regex &CommentPragmasRegex) const {
return Split(StringRef::npos, 0);
}
@@ -255,7 +255,7 @@ class BreakableStringLiteral : public BreakableToken {
Split getSplit(unsigned LineIndex, unsigned TailOffset, unsigned ColumnLimit,
unsigned ContentStartColumn,
- llvm::Regex &CommentPragmasRegex) const override;
+ const llvm::Regex &CommentPragmasRegex) const override;
void insertBreak(unsigned LineIndex, unsigned TailOffset, Split Split,
unsigned ContentIndent,
WhitespaceManager &Whitespaces) const override;
@@ -298,7 +298,7 @@ class BreakableComment : public BreakableToken {
unsigned getLineCount() const override;
Split getSplit(unsigned LineIndex, unsigned TailOffset, unsigned ColumnLimit,
unsigned ContentStartColumn,
- llvm::Regex &CommentPragmasRegex) const override;
+ const llvm::Regex &CommentPragmasRegex) const override;
void compressWhitespace(unsigned LineIndex, unsigned TailOffset, Split Split,
WhitespaceManager &Whitespaces) const override;
@@ -309,7 +309,7 @@ class BreakableComment : public BreakableToken {
// Checks if the content of line LineIndex may be reflown with the previous
// line.
virtual bool mayReflow(unsigned LineIndex,
- llvm::Regex &CommentPragmasRegex) const = 0;
+ const llvm::Regex &CommentPragmasRegex) const = 0;
// Contains the original text of the lines of the block comment.
//
@@ -363,7 +363,7 @@ class BreakableBlockComment : public BreakableComment {
Split getSplit(unsigned LineIndex, unsigned TailOffset, unsigned ColumnLimit,
unsigned ContentStartColumn,
- llvm::Regex &CommentPragmasRegex) const override;
+ const llvm::Regex &CommentPragmasRegex) const override;
unsigned getRangeLength(unsigned LineIndex, unsigned Offset,
StringRef::size_type Length,
unsigned StartColumn) const override;
@@ -375,7 +375,7 @@ class BreakableBlockComment : public BreakableComment {
unsigned ContentIndent,
WhitespaceManager &Whitespaces) const override;
Split getReflowSplit(unsigned LineIndex,
- llvm::Regex &CommentPragmasRegex) const override;
+ const llvm::Regex &CommentPragmasRegex) const override;
void reflow(unsigned LineIndex,
WhitespaceManager &Whitespaces) const override;
bool introducesBreakBeforeToken() const override;
@@ -384,7 +384,7 @@ class BreakableBlockComment : public BreakableComment {
Split getSplitAfterLastLine(unsigned TailOffset) const override;
bool mayReflow(unsigned LineIndex,
- llvm::Regex &CommentPragmasRegex) const override;
+ const llvm::Regex &CommentPragmasRegex) const override;
// Contains Javadoc annotations that require additional indent when continued
// on multiple lines.
@@ -448,14 +448,14 @@ class BreakableLineCommentSection : public BreakableComment {
unsigned ContentIndent,
WhitespaceManager &Whitespaces) const override;
Split getReflowSplit(unsigned LineIndex,
- llvm::Regex &CommentPragmasRegex) const override;
+ const llvm::Regex &CommentPragmasRegex) const override;
void reflow(unsigned LineIndex,
WhitespaceManager &Whitespaces) const override;
void adaptStartOfLine(unsigned LineIndex,
WhitespaceManager &Whitespaces) const override;
void updateNextToken(LineState &State) const override;
bool mayReflow(unsigned LineIndex,
- llvm::Regex &CommentPragmasRegex) const override;
+ const llvm::Regex &CommentPragmasRegex) const override;
private:
// OriginalPrefix[i] contains the original prefix of line i, including
diff --git a/clang/lib/Format/NamespaceEndCommentsFixer.cpp b/clang/lib/Format/NamespaceEndCommentsFixer.cpp
index 98901cff2681..20b424f86077 100644
--- a/clang/lib/Format/NamespaceEndCommentsFixer.cpp
+++ b/clang/lib/Format/NamespaceEndCommentsFixer.cpp
@@ -92,24 +92,24 @@ bool validEndComment(const FormatToken *RBraceTok, StringRef NamespaceName,
// Matches a valid namespace end comment.
// Valid namespace end comments don't need to be edited.
- static llvm::Regex *const NamespaceCommentPattern =
- new llvm::Regex("^/[/*] *(end (of )?)? *(anonymous|unnamed)? *"
- "namespace( +([a-zA-Z0-9:_]+))?\\.? *(\\*/)?$",
- llvm::Regex::IgnoreCase);
- static llvm::Regex *const NamespaceMacroCommentPattern =
- new llvm::Regex("^/[/*] *(end (of )?)? *(anonymous|unnamed)? *"
- "([a-zA-Z0-9_]+)\\(([a-zA-Z0-9:_]*)\\)\\.? *(\\*/)?$",
- llvm::Regex::IgnoreCase);
+ static const llvm::Regex NamespaceCommentPattern =
+ llvm::Regex("^/[/*] *(end (of )?)? *(anonymous|unnamed)? *"
+ "namespace( +([a-zA-Z0-9:_]+))?\\.? *(\\*/)?$",
+ llvm::Regex::IgnoreCase);
+ static const llvm::Regex NamespaceMacroCommentPattern =
+ llvm::Regex("^/[/*] *(end (of )?)? *(anonymous|unnamed)? *"
+ "([a-zA-Z0-9_]+)\\(([a-zA-Z0-9:_]*)\\)\\.? *(\\*/)?$",
+ llvm::Regex::IgnoreCase);
SmallVector<StringRef, 8> Groups;
if (NamespaceTok->is(TT_NamespaceMacro) &&
- NamespaceMacroCommentPattern->match(Comment->TokenText, &Groups)) {
+ NamespaceMacroCommentPattern.match(Comment->TokenText, &Groups)) {
StringRef NamespaceTokenText = Groups.size() > 4 ? Groups[4] : "";
// The name of the macro must be used.
if (NamespaceTokenText != NamespaceTok->TokenText)
return false;
} else if (NamespaceTok->isNot(tok::kw_namespace) ||
- !NamespaceCommentPattern->match(Comment->TokenText, &Groups)) {
+ !NamespaceCommentPattern.match(Comment->TokenText, &Groups)) {
// Comment does not match regex.
return false;
}
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index bbe05602f6da..1e27eab1e744 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -2498,9 +2498,10 @@ bool UnwrappedLineParser::isOnNewLine(const FormatToken &FormatTok) {
// Checks if \p FormatTok is a line comment that continues the line comment
// section on \p Line.
-static bool continuesLineCommentSection(const FormatToken &FormatTok,
- const UnwrappedLine &Line,
- llvm::Regex &CommentPragmasRegex) {
+static bool
+continuesLineCommentSection(const FormatToken &FormatTok,
+ const UnwrappedLine &Line,
+ const llvm::Regex &CommentPragmasRegex) {
if (Line.Tokens.empty())
return false;
diff --git a/clang/tools/clang-refactor/TestSupport.cpp b/clang/tools/clang-refactor/TestSupport.cpp
index 9d2d9662276a..cfe9c0c8427b 100644
--- a/clang/tools/clang-refactor/TestSupport.cpp
+++ b/clang/tools/clang-refactor/TestSupport.cpp
@@ -303,9 +303,10 @@ findTestSelectionRanges(StringRef Filename) {
// See the doc comment for this function for the explanation of this
// syntax.
- static Regex RangeRegex("range[[:blank:]]*([[:alpha:]_]*)?[[:blank:]]*=[[:"
- "blank:]]*(\\+[[:digit:]]+)?[[:blank:]]*(->[[:blank:]"
- "]*[\\+\\:[:digit:]]+)?");
+ static const Regex RangeRegex(
+ "range[[:blank:]]*([[:alpha:]_]*)?[[:blank:]]*=[[:"
+ "blank:]]*(\\+[[:digit:]]+)?[[:blank:]]*(->[[:blank:]"
+ "]*[\\+\\:[:digit:]]+)?");
std::map<std::string, SmallVector<TestSelectionRange, 8>> GroupedRanges;
@@ -352,7 +353,7 @@ findTestSelectionRanges(StringRef Filename) {
unsigned EndOffset;
if (!Matches[3].empty()) {
- static Regex EndLocRegex(
+ static const Regex EndLocRegex(
"->[[:blank:]]*(\\+[[:digit:]]+):([[:digit:]]+)");
SmallVector<StringRef, 4> EndLocMatches;
if (!EndLocRegex.match(Matches[3], &EndLocMatches)) {
More information about the cfe-commits
mailing list