Skip to content

[clang-format] Limit how much work guessLanguage() can do #78925

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2830,7 +2830,7 @@ class Cleaner : public TokenAnalyzer {
class ObjCHeaderStyleGuesser : public TokenAnalyzer {
public:
ObjCHeaderStyleGuesser(const Environment &Env, const FormatStyle &Style)
: TokenAnalyzer(Env, Style), IsObjC(false) {}
: TokenAnalyzer(Env, Style, MaxLinesToProcess), IsObjC(false) {}

std::pair<tooling::Replacements, unsigned>
analyze(TokenAnnotator &Annotator,
Expand All @@ -2846,6 +2846,12 @@ class ObjCHeaderStyleGuesser : public TokenAnalyzer {
bool isObjC() { return IsObjC; }

private:
// Limit the number of variants of the file TokenAnalyzer processes for
// the purpose of guessing the language. An inaccurate guess is better than
// hanging for a long time or OOMing, which has been observed with real
// libraries which are single-header with many preprocessor branches.
static const unsigned MaxLinesToProcess = (1 << 20);

static bool
guessIsObjC(const SourceManager &SourceManager,
const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines,
Expand Down
8 changes: 5 additions & 3 deletions clang/lib/Format/TokenAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,14 @@ Environment::Environment(StringRef Code, StringRef FileName,
ID(VirtualSM->get().getMainFileID()), FirstStartColumn(FirstStartColumn),
NextStartColumn(NextStartColumn), LastStartColumn(LastStartColumn) {}

TokenAnalyzer::TokenAnalyzer(const Environment &Env, const FormatStyle &Style)
TokenAnalyzer::TokenAnalyzer(const Environment &Env, const FormatStyle &Style,
unsigned MaxLinesToProcess)
: Style(Style), Env(Env),
AffectedRangeMgr(Env.getSourceManager(), Env.getCharRanges()),
UnwrappedLines(1),
Encoding(encoding::detectEncoding(
Env.getSourceManager().getBufferData(Env.getFileID()))) {
Env.getSourceManager().getBufferData(Env.getFileID()))),
MaxLinesToProcess(MaxLinesToProcess) {
LLVM_DEBUG(
llvm::dbgs() << "File encoding: "
<< (Encoding == encoding::Encoding_UTF8 ? "UTF8" : "unknown")
Expand All @@ -109,7 +111,7 @@ TokenAnalyzer::process(bool SkipAnnotation) {
SmallVector<FormatToken *, 10> Tokens(Toks.begin(), Toks.end());
UnwrappedLineParser Parser(Env.getSourceManager(), Style, Lex.getKeywords(),
Env.getFirstStartColumn(), Tokens, *this,
Allocator, IdentTable);
Allocator, IdentTable, MaxLinesToProcess);
Parser.parse();
assert(UnwrappedLines.back().empty());
unsigned Penalty = 0;
Expand Down
4 changes: 3 additions & 1 deletion clang/lib/Format/TokenAnalyzer.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ class Environment {

class TokenAnalyzer : public UnwrappedLineConsumer {
public:
TokenAnalyzer(const Environment &Env, const FormatStyle &Style);
TokenAnalyzer(const Environment &Env, const FormatStyle &Style,
unsigned MaxLinesToProcess = 0);

std::pair<tooling::Replacements, unsigned>
process(bool SkipAnnotation = false);
Expand All @@ -109,6 +110,7 @@ class TokenAnalyzer : public UnwrappedLineConsumer {
AffectedRangeManager AffectedRangeMgr;
SmallVector<SmallVector<UnwrappedLine, 16>, 2> UnwrappedLines;
encoding::Encoding Encoding;
unsigned MaxLinesToProcess;
};

} // end namespace format
Expand Down
11 changes: 9 additions & 2 deletions clang/lib/Format/UnwrappedLineParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ UnwrappedLineParser::UnwrappedLineParser(
const AdditionalKeywords &Keywords, unsigned FirstStartColumn,
ArrayRef<FormatToken *> Tokens, UnwrappedLineConsumer &Callback,
llvm::SpecificBumpPtrAllocator<FormatToken> &Allocator,
IdentifierTable &IdentTable)
IdentifierTable &IdentTable, unsigned MaxLinesToProcess)
: Line(new UnwrappedLine), MustBreakBeforeNextToken(false),
CurrentLines(&Lines), Style(Style), Keywords(Keywords),
CommentPragmasRegex(Style.CommentPragmas), Tokens(nullptr),
Expand All @@ -160,7 +160,8 @@ UnwrappedLineParser::UnwrappedLineParser(
? IG_Rejected
: IG_Inited),
IncludeGuardToken(nullptr), FirstStartColumn(FirstStartColumn),
Macros(Style.Macros, SourceMgr, Style, Allocator, IdentTable) {}
Macros(Style.Macros, SourceMgr, Style, Allocator, IdentTable),
MaxLinesToProcess(MaxLinesToProcess) {}

void UnwrappedLineParser::reset() {
PPBranchLevel = -1;
Expand Down Expand Up @@ -194,6 +195,7 @@ void UnwrappedLineParser::reset() {
void UnwrappedLineParser::parse() {
IndexedTokenSource TokenSource(AllTokens);
Line->FirstStartColumn = FirstStartColumn;
size_t TotalLinesProcessed = 0;
do {
LLVM_DEBUG(llvm::dbgs() << "----\n");
reset();
Expand Down Expand Up @@ -235,6 +237,7 @@ void UnwrappedLineParser::parse() {
Callback.consumeUnwrappedLine(Line);
}
Callback.finishRun();
TotalLinesProcessed += ExpandedLines.size();
}

LLVM_DEBUG(llvm::dbgs() << "Unwrapped lines:\n");
Expand All @@ -243,6 +246,10 @@ void UnwrappedLineParser::parse() {
Callback.consumeUnwrappedLine(Line);
}
Callback.finishRun();
TotalLinesProcessed += Lines.size();
if (MaxLinesToProcess > 0 && TotalLinesProcessed >= MaxLinesToProcess) {
break;
}
Lines.clear();
while (!PPLevelBranchIndex.empty() &&
PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
Expand Down
8 changes: 7 additions & 1 deletion clang/lib/Format/UnwrappedLineParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ class UnwrappedLineParser {
unsigned FirstStartColumn, ArrayRef<FormatToken *> Tokens,
UnwrappedLineConsumer &Callback,
llvm::SpecificBumpPtrAllocator<FormatToken> &Allocator,
IdentifierTable &IdentTable);
IdentifierTable &IdentTable,
unsigned MaxLinesToProcess = 0);

void parse();

Expand Down Expand Up @@ -406,6 +407,11 @@ class UnwrappedLineParser {

MacroExpander Macros;

// If set to a nonzero value, stop generating new runs after this
// many lines (summed over all the runs generated so far) have been
// processed.
unsigned MaxLinesToProcess;

friend class ScopedLineState;
friend class CompoundStatementIndenter;
};
Expand Down