Skip to content

Commit 8db03e8

Browse files
committed
merge main into amd-staging
2 parents aa20d53 + 4e44bd0 commit 8db03e8

File tree

175 files changed

+6098
-5014
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

175 files changed

+6098
-5014
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4782,7 +4782,13 @@ the configuration (without a prefix: ``Auto``).
47824782
.. _Language:
47834783

47844784
**Language** (``LanguageKind``) :versionbadge:`clang-format 3.5` :ref:`<Language>`
4785-
Language, this format style is targeted at.
4785+
The language that this format style targets.
4786+
4787+
.. note::
4788+
4789+
You can also specify the language (``Cpp`` or ``ObjC``) for ``.h`` files
4790+
by adding a ``// clang-format Language:`` line before the first
4791+
non-comment (and non-empty) line, e.g. ``// clang-format Language: Cpp``.
47864792

47874793
Possible values:
47884794

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,9 @@ clang-format
271271
- Adds ``BreakBeforeTemplateCloser`` option.
272272
- Adds ``BinPackLongBracedList`` option to override bin packing options in
273273
long (20 item or more) braced list initializer lists.
274+
- Allow specifying the language (C++ or Objective-C) for a ``.h`` file by adding
275+
a special comment (e.g. ``// clang-format Language: ObjC``) near the top of
276+
the file.
274277

275278
libclang
276279
--------

clang/include/clang/Format/Format.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3353,7 +3353,12 @@ struct FormatStyle {
33533353
}
33543354
bool isTableGen() const { return Language == LK_TableGen; }
33553355

3356-
/// Language, this format style is targeted at.
3356+
/// The language that this format style targets.
3357+
/// \note
3358+
/// You can also specify the language (``Cpp`` or ``ObjC``) for ``.h`` files
3359+
/// by adding a ``// clang-format Language:`` line before the first
3360+
/// non-comment (and non-empty) line, e.g. ``// clang-format Language: Cpp``.
3361+
/// \endnote
33573362
/// \version 3.5
33583363
LanguageKind Language;
33593364

clang/include/clang/StaticAnalyzer/Checkers/SValExplainer.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ namespace ento {
2727
class SValExplainer : public FullSValVisitor<SValExplainer, std::string> {
2828
private:
2929
ASTContext &ACtx;
30+
ProgramStateRef State;
3031

3132
std::string printStmt(const Stmt *S) {
3233
std::string Str;
@@ -55,7 +56,8 @@ class SValExplainer : public FullSValVisitor<SValExplainer, std::string> {
5556
}
5657

5758
public:
58-
SValExplainer(ASTContext &Ctx) : ACtx(Ctx) {}
59+
SValExplainer(ASTContext &Ctx, ProgramStateRef State)
60+
: ACtx(Ctx), State(State) {}
5961

6062
std::string VisitUnknownVal(UnknownVal V) {
6163
return "unknown value";
@@ -166,7 +168,7 @@ class SValExplainer : public FullSValVisitor<SValExplainer, std::string> {
166168
.getCanonicalType()->getAs<ObjCObjectPointerType>())
167169
return "object at " + Visit(R->getSymbol());
168170
// Other heap-based symbolic regions are also special.
169-
if (isa<HeapSpaceRegion>(R->getMemorySpace()))
171+
if (R->hasMemorySpace<HeapSpaceRegion>(State))
170172
return "heap segment that starts at " + Visit(R->getSymbol());
171173
return "pointee of " + Visit(R->getSymbol());
172174
}

clang/include/clang/StaticAnalyzer/Core/PathSensitive/MemRegion.h

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "clang/Analysis/AnalysisDeclContext.h"
2727
#include "clang/Basic/LLVM.h"
2828
#include "clang/Basic/SourceLocation.h"
29+
#include "clang/StaticAnalyzer/Core/PathSensitive/ProgramState_Fwd.h"
2930
#include "clang/StaticAnalyzer/Core/PathSensitive/SVals.h"
3031
#include "clang/StaticAnalyzer/Core/PathSensitive/SymExpr.h"
3132
#include "llvm/ADT/DenseMap.h"
@@ -119,7 +120,40 @@ class MemRegion : public llvm::FoldingSetNode {
119120

120121
virtual MemRegionManager &getMemRegionManager() const = 0;
121122

122-
LLVM_ATTRIBUTE_RETURNS_NONNULL const MemSpaceRegion *getMemorySpace() const;
123+
/// Deprecated. Gets the 'raw' memory space of a memory region's base region.
124+
/// If the MemRegion is originally associated with Unknown memspace, then the
125+
/// State may have a more accurate memspace for this region.
126+
/// Use getMemorySpace(ProgramStateRef) instead.
127+
[[nodiscard]] LLVM_ATTRIBUTE_RETURNS_NONNULL const MemSpaceRegion *
128+
getRawMemorySpace() const;
129+
130+
/// Deprecated. Use getMemorySpace(ProgramStateRef) instead.
131+
template <class MemSpace>
132+
[[nodiscard]] const MemSpace *getRawMemorySpaceAs() const {
133+
return dyn_cast<MemSpace>(getRawMemorySpace());
134+
}
135+
136+
/// Returns the most specific memory space for this memory region in the given
137+
/// ProgramStateRef. We may infer a more accurate memory space for unknown
138+
/// space regions and associate this in the State.
139+
[[nodiscard]] LLVM_ATTRIBUTE_RETURNS_NONNULL const MemSpaceRegion *
140+
getMemorySpace(ProgramStateRef State) const;
141+
142+
template <class MemSpace>
143+
[[nodiscard]] const MemSpace *getMemorySpaceAs(ProgramStateRef State) const {
144+
return dyn_cast<MemSpace>(getMemorySpace(State));
145+
}
146+
147+
template <typename... MemorySpaces>
148+
[[nodiscard]] bool hasMemorySpace(ProgramStateRef State) const {
149+
static_assert(sizeof...(MemorySpaces));
150+
return isa<MemorySpaces...>(getMemorySpace(State));
151+
}
152+
153+
/// Set the dynamically deduced memory space of a MemRegion that currently has
154+
/// UnknownSpaceRegion. \p Space shouldn't be UnknownSpaceRegion.
155+
[[nodiscard]] ProgramStateRef
156+
setMemorySpace(ProgramStateRef State, const MemSpaceRegion *Space) const;
123157

124158
LLVM_ATTRIBUTE_RETURNS_NONNULL const MemRegion *getBaseRegion() const;
125159

@@ -140,12 +174,6 @@ class MemRegion : public llvm::FoldingSetNode {
140174
/// It might return null.
141175
const SymbolicRegion *getSymbolicBase() const;
142176

143-
bool hasStackStorage() const;
144-
145-
bool hasStackNonParametersStorage() const;
146-
147-
bool hasStackParametersStorage() const;
148-
149177
/// Compute the offset within the top level memory object.
150178
RegionOffset getAsOffset() const;
151179

clang/lib/AST/ByteCode/Interp.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,11 @@ bool CheckCallable(InterpState &S, CodePtr OpPC, const Function *F) {
732732
DiagDecl = CD = Inherited;
733733
}
734734

735+
// Silently reject constructors of invalid classes. The invalid class
736+
// has been rejected elsewhere before.
737+
if (CD && CD->getParent()->isInvalidDecl())
738+
return false;
739+
735740
// FIXME: If DiagDecl is an implicitly-declared special member function
736741
// or an inheriting constructor, we should be much more explicit about why
737742
// it's not constexpr.

clang/lib/Format/Format.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4021,6 +4021,33 @@ static FormatStyle::LanguageKind getLanguageByFileName(StringRef FileName) {
40214021
return FormatStyle::LK_Cpp;
40224022
}
40234023

4024+
static FormatStyle::LanguageKind getLanguageByComment(const Environment &Env) {
4025+
const auto ID = Env.getFileID();
4026+
const auto &SourceMgr = Env.getSourceManager();
4027+
4028+
LangOptions LangOpts;
4029+
LangOpts.CPlusPlus = 1;
4030+
LangOpts.LineComment = 1;
4031+
4032+
Lexer Lex(ID, SourceMgr.getBufferOrFake(ID), SourceMgr, LangOpts);
4033+
Lex.SetCommentRetentionState(true);
4034+
4035+
for (Token Tok; !Lex.LexFromRawLexer(Tok) && Tok.is(tok::comment);) {
4036+
auto Text = StringRef(SourceMgr.getCharacterData(Tok.getLocation()),
4037+
Tok.getLength());
4038+
if (!Text.consume_front("// clang-format Language:"))
4039+
continue;
4040+
4041+
Text = Text.trim();
4042+
if (Text == "Cpp")
4043+
return FormatStyle::LK_Cpp;
4044+
if (Text == "ObjC")
4045+
return FormatStyle::LK_ObjC;
4046+
}
4047+
4048+
return FormatStyle::LK_None;
4049+
}
4050+
40244051
FormatStyle::LanguageKind guessLanguage(StringRef FileName, StringRef Code) {
40254052
const auto GuessedLanguage = getLanguageByFileName(FileName);
40264053
if (GuessedLanguage == FormatStyle::LK_Cpp) {
@@ -4030,6 +4057,10 @@ FormatStyle::LanguageKind guessLanguage(StringRef FileName, StringRef Code) {
40304057
if (!Code.empty() && (Extension.empty() || Extension == ".h")) {
40314058
auto NonEmptyFileName = FileName.empty() ? "guess.h" : FileName;
40324059
Environment Env(Code, NonEmptyFileName, /*Ranges=*/{});
4060+
if (const auto Language = getLanguageByComment(Env);
4061+
Language != FormatStyle::LK_None) {
4062+
return Language;
4063+
}
40334064
ObjCHeaderStyleGuesser Guesser(Env, getLLVMStyle());
40344065
Guesser.process();
40354066
if (Guesser.isObjC())

clang/lib/Headers/intrin.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,6 @@ void _Store_HLERelease(long volatile *, long);
162162
void _Store64_HLERelease(__int64 volatile *, __int64);
163163
void _StorePointer_HLERelease(void *volatile *, void *);
164164
void _WriteBarrier(void);
165-
unsigned __int32 xbegin(void);
166-
void _xend(void);
167165

168166
/* These additional intrinsics are turned on in x64/amd64/x86_64 mode. */
169167
#if defined(__x86_64__) && !defined(__arm64ec__)

clang/lib/StaticAnalyzer/Checkers/ArrayBoundChecker.cpp

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ determineElementSize(const std::optional<QualType> T, const CheckerContext &C) {
7878
}
7979

8080
class StateUpdateReporter {
81+
const MemSpaceRegion *Space;
8182
const SubRegion *Reg;
8283
const NonLoc ByteOffsetVal;
8384
const std::optional<QualType> ElementType;
@@ -88,8 +89,8 @@ class StateUpdateReporter {
8889
public:
8990
StateUpdateReporter(const SubRegion *R, NonLoc ByteOffsVal, const Expr *E,
9091
CheckerContext &C)
91-
: Reg(R), ByteOffsetVal(ByteOffsVal),
92-
ElementType(determineElementType(E, C)),
92+
: Space(R->getMemorySpace(C.getState())), Reg(R),
93+
ByteOffsetVal(ByteOffsVal), ElementType(determineElementType(E, C)),
9394
ElementSize(determineElementSize(ElementType, C)) {}
9495

9596
void recordNonNegativeAssumption() { AssumedNonNegative = true; }
@@ -352,7 +353,8 @@ compareValueToThreshold(ProgramStateRef State, NonLoc Value, NonLoc Threshold,
352353
return {nullptr, nullptr};
353354
}
354355

355-
static std::string getRegionName(const SubRegion *Region) {
356+
static std::string getRegionName(const MemSpaceRegion *Space,
357+
const SubRegion *Region) {
356358
if (std::string RegName = Region->getDescriptiveName(); !RegName.empty())
357359
return RegName;
358360

@@ -367,8 +369,7 @@ static std::string getRegionName(const SubRegion *Region) {
367369
if (isa<AllocaRegion>(Region))
368370
return "the memory returned by 'alloca'";
369371

370-
if (isa<SymbolicRegion>(Region) &&
371-
isa<HeapSpaceRegion>(Region->getMemorySpace()))
372+
if (isa<SymbolicRegion>(Region) && isa<HeapSpaceRegion>(Space))
372373
return "the heap area";
373374

374375
if (isa<StringRegion>(Region))
@@ -388,8 +389,9 @@ static std::optional<int64_t> getConcreteValue(std::optional<NonLoc> SV) {
388389
return SV ? getConcreteValue(*SV) : std::nullopt;
389390
}
390391

391-
static Messages getPrecedesMsgs(const SubRegion *Region, NonLoc Offset) {
392-
std::string RegName = getRegionName(Region), OffsetStr = "";
392+
static Messages getPrecedesMsgs(const MemSpaceRegion *Space,
393+
const SubRegion *Region, NonLoc Offset) {
394+
std::string RegName = getRegionName(Space, Region), OffsetStr = "";
393395

394396
if (auto ConcreteOffset = getConcreteValue(Offset))
395397
OffsetStr = formatv(" {0}", ConcreteOffset);
@@ -418,10 +420,11 @@ static bool tryDividePair(std::optional<int64_t> &Val1,
418420
return true;
419421
}
420422

421-
static Messages getExceedsMsgs(ASTContext &ACtx, const SubRegion *Region,
422-
NonLoc Offset, NonLoc Extent, SVal Location,
423+
static Messages getExceedsMsgs(ASTContext &ACtx, const MemSpaceRegion *Space,
424+
const SubRegion *Region, NonLoc Offset,
425+
NonLoc Extent, SVal Location,
423426
bool AlsoMentionUnderflow) {
424-
std::string RegName = getRegionName(Region);
427+
std::string RegName = getRegionName(Space, Region);
425428
const auto *EReg = Location.getAsRegion()->getAs<ElementRegion>();
426429
assert(EReg && "this checker only handles element access");
427430
QualType ElemType = EReg->getElementType();
@@ -468,9 +471,10 @@ static Messages getExceedsMsgs(ASTContext &ACtx, const SubRegion *Region,
468471
std::string(Buf)};
469472
}
470473

471-
static Messages getTaintMsgs(const SubRegion *Region, const char *OffsetName,
474+
static Messages getTaintMsgs(const MemSpaceRegion *Space,
475+
const SubRegion *Region, const char *OffsetName,
472476
bool AlsoMentionUnderflow) {
473-
std::string RegName = getRegionName(Region);
477+
std::string RegName = getRegionName(Space, Region);
474478
return {formatv("Potential out of bound access to {0} with tainted {1}",
475479
RegName, OffsetName),
476480
formatv("Access of {0} with a tainted {1} that may be {2}too large",
@@ -539,7 +543,7 @@ std::string StateUpdateReporter::getMessage(PathSensitiveBugReport &BR) const {
539543
<< "' elements in ";
540544
else
541545
Out << "the extent of ";
542-
Out << getRegionName(Reg);
546+
Out << getRegionName(Space, Reg);
543547
}
544548
return std::string(Out.str());
545549
}
@@ -589,7 +593,7 @@ void ArrayBoundChecker::performCheck(const Expr *E, CheckerContext &C) const {
589593
StateUpdateReporter SUR(Reg, ByteOffset, E, C);
590594

591595
// CHECK LOWER BOUND
592-
const MemSpaceRegion *Space = Reg->getMemorySpace();
596+
const MemSpaceRegion *Space = Reg->getMemorySpace(State);
593597
if (!(isa<SymbolicRegion>(Reg) && isa<UnknownSpaceRegion>(Space))) {
594598
// A symbolic region in unknown space represents an unknown pointer that
595599
// may point into the middle of an array, so we don't look for underflows.
@@ -632,7 +636,7 @@ void ArrayBoundChecker::performCheck(const Expr *E, CheckerContext &C) const {
632636
} else {
633637
if (!WithinLowerBound) {
634638
// ...and it cannot be valid (>= 0), so report an error.
635-
Messages Msgs = getPrecedesMsgs(Reg, ByteOffset);
639+
Messages Msgs = getPrecedesMsgs(Space, Reg, ByteOffset);
636640
reportOOB(C, PrecedesLowerBound, Msgs, ByteOffset, std::nullopt);
637641
return;
638642
}
@@ -675,8 +679,8 @@ void ArrayBoundChecker::performCheck(const Expr *E, CheckerContext &C) const {
675679
}
676680

677681
Messages Msgs =
678-
getExceedsMsgs(C.getASTContext(), Reg, ByteOffset, *KnownSize,
679-
Location, AlsoMentionUnderflow);
682+
getExceedsMsgs(C.getASTContext(), Space, Reg, ByteOffset,
683+
*KnownSize, Location, AlsoMentionUnderflow);
680684
reportOOB(C, ExceedsUpperBound, Msgs, ByteOffset, KnownSize);
681685
return;
682686
}
@@ -692,7 +696,8 @@ void ArrayBoundChecker::performCheck(const Expr *E, CheckerContext &C) const {
692696
if (isTainted(State, ASE->getIdx(), C.getLocationContext()))
693697
OffsetName = "index";
694698

695-
Messages Msgs = getTaintMsgs(Reg, OffsetName, AlsoMentionUnderflow);
699+
Messages Msgs =
700+
getTaintMsgs(Space, Reg, OffsetName, AlsoMentionUnderflow);
696701
reportOOB(C, ExceedsUpperBound, Msgs, ByteOffset, KnownSize,
697702
/*IsTaintBug=*/true);
698703
return;

clang/lib/StaticAnalyzer/Checkers/ErrnoChecker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ ProgramStateRef ErrnoChecker::checkRegionChanges(
231231

232232
// Always reset errno state when the system memory space is invalidated.
233233
// The ErrnoRegion is not always found in the list in this case.
234-
if (llvm::is_contained(Regions, ErrnoRegion->getMemorySpace()))
234+
if (llvm::is_contained(Regions, ErrnoRegion->getMemorySpace(State)))
235235
return clearErrnoState(State);
236236

237237
return State;

0 commit comments

Comments
 (0)