Skip to content

Commit c813667

Browse files
authored
[clang] Fix static analyzer concerns in #embed code (#99331)
1. Dead code in `LookupEmbedFile`. The loop always exited on the first iteration. This was also causing a bug of not checking all directories provided by `--embed-dir`. 2. Use of uninitialized variable `CurTok` in `LexEmbedParameters`. It was used to initialize the field which seems to be unused. Removed unused field, this way `CurTok` should be initialized by Lex method.
1 parent c248d05 commit c813667

File tree

3 files changed

+7
-6
lines changed

3 files changed

+7
-6
lines changed

clang/include/clang/Lex/PPEmbedParameters.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ struct LexEmbedParametersResult {
7575
std::optional<PPEmbedParameterIfEmpty> MaybeIfEmptyParam;
7676
std::optional<PPEmbedParameterPrefix> MaybePrefixParam;
7777
std::optional<PPEmbedParameterSuffix> MaybeSuffixParam;
78-
SourceRange ParamRange;
7978
int UnrecognizedParams;
8079

8180
size_t PrefixTokenCount() const {

clang/lib/Lex/PPDirectives.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,7 +1137,9 @@ Preprocessor::LookupEmbedFile(StringRef Filename, bool isAngled, bool OpenFile,
11371137
SeparateComponents(LookupPath, Entry, Filename, false);
11381138
llvm::Expected<FileEntryRef> ShouldBeEntry =
11391139
FM.getFileRef(LookupPath, OpenFile);
1140-
return llvm::expectedToOptional(std::move(ShouldBeEntry));
1140+
if (ShouldBeEntry)
1141+
return llvm::expectedToOptional(std::move(ShouldBeEntry));
1142+
llvm::consumeError(ShouldBeEntry.takeError());
11411143
}
11421144
return std::nullopt;
11431145
}
@@ -3624,12 +3626,10 @@ Preprocessor::LexEmbedParameters(Token &CurTok, bool ForHasEmbed) {
36243626
LexEmbedParametersResult Result{};
36253627
SmallVector<Token, 2> ParameterTokens;
36263628
tok::TokenKind EndTokenKind = ForHasEmbed ? tok::r_paren : tok::eod;
3627-
Result.ParamRange = {CurTok.getLocation(), CurTok.getLocation()};
36283629

36293630
auto DiagMismatchedBracesAndSkipToEOD =
36303631
[&](tok::TokenKind Expected,
36313632
std::pair<tok::TokenKind, SourceLocation> Matches) {
3632-
Result.ParamRange.setEnd(CurTok.getEndLoc());
36333633
Diag(CurTok, diag::err_expected) << Expected;
36343634
Diag(Matches.second, diag::note_matching) << Matches.first;
36353635
if (CurTok.isNot(tok::eod))
@@ -3638,7 +3638,6 @@ Preprocessor::LexEmbedParameters(Token &CurTok, bool ForHasEmbed) {
36383638

36393639
auto ExpectOrDiagAndSkipToEOD = [&](tok::TokenKind Kind) {
36403640
if (CurTok.isNot(Kind)) {
3641-
Result.ParamRange.setEnd(CurTok.getEndLoc());
36423641
Diag(CurTok, diag::err_expected) << Kind;
36433642
if (CurTok.isNot(tok::eod))
36443643
DiscardUntilEndOfDirective(CurTok);
@@ -3872,7 +3871,6 @@ Preprocessor::LexEmbedParameters(Token &CurTok, bool ForHasEmbed) {
38723871
}
38733872
}
38743873
}
3875-
Result.ParamRange.setEnd(CurTok.getLocation());
38763874
return Result;
38773875
}
38783876

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// RUN: %clang_cc1 -std=c23 %s -E -verify --embed-dir=%S --embed-dir=%S/Inputs
2+
// expected-no-diagnostics
3+
4+
#embed <jk.txt>

0 commit comments

Comments
 (0)