Skip to content

Commit 03c4267

Browse files
authored
Merge pull request #105 from AMD-Lightning-Internal/upstream_merge_202501142308
merge main into amd-staging
2 parents a898d5f + 6a7c018 commit 03c4267

File tree

128 files changed

+5770
-891
lines changed

Some content is hidden

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

128 files changed

+5770
-891
lines changed

clang-tools-extra/clang-tidy/tool/clang-tidy-diff.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import tempfile
3636
import threading
3737
import traceback
38+
from pathlib import Path
3839

3940
try:
4041
import yaml
@@ -124,6 +125,23 @@ def merge_replacement_files(tmpdir, mergefile):
124125
open(mergefile, "w").close()
125126

126127

128+
def get_compiling_files(args):
129+
"""Read a compile_commands.json database and return a set of file paths"""
130+
current_dir = Path.cwd()
131+
compile_commands_json = (
132+
(current_dir / args.build_path) if args.build_path else current_dir
133+
)
134+
compile_commands_json = compile_commands_json / "compile_commands.json"
135+
files = set()
136+
with open(compile_commands_json) as db_file:
137+
db_json = json.load(db_file)
138+
for entry in db_json:
139+
if "file" not in entry:
140+
continue
141+
files.add(Path(entry["file"]))
142+
return files
143+
144+
127145
def main():
128146
parser = argparse.ArgumentParser(
129147
description="Run clang-tidy against changed files, and "
@@ -234,6 +252,13 @@ def main():
234252
action="store_true",
235253
help="Allow empty enabled checks.",
236254
)
255+
parser.add_argument(
256+
"-only-check-in-db",
257+
dest="skip_non_compiling",
258+
default=False,
259+
action="store_true",
260+
help="Only check files in the compilation database",
261+
)
237262

238263
clang_tidy_args = []
239264
argv = sys.argv[1:]
@@ -243,11 +268,13 @@ def main():
243268

244269
args = parser.parse_args(argv)
245270

271+
compiling_files = get_compiling_files(args) if args.skip_non_compiling else None
272+
246273
# Extract changed lines for each file.
247274
filename = None
248275
lines_by_file = {}
249276
for line in sys.stdin:
250-
match = re.search('^\\+\\+\\+\\ "?(.*?/){%s}([^ \t\n"]*)' % args.p, line)
277+
match = re.search(r'^\+\+\+\ "?(.*?/){%s}([^ \t\n"]*)' % args.p, line)
251278
if match:
252279
filename = match.group(2)
253280
if filename is None:
@@ -260,6 +287,13 @@ def main():
260287
if not re.match("^%s$" % args.iregex, filename, re.IGNORECASE):
261288
continue
262289

290+
# Skip any files not in the compiling list
291+
if (
292+
compiling_files is not None
293+
and (Path.cwd() / filename) not in compiling_files
294+
):
295+
continue
296+
263297
match = re.search(r"^@@.*\+(\d+)(,(\d+))?", line)
264298
if match:
265299
start_line = int(match.group(1))

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ Improvements to clang-query
108108
Improvements to clang-tidy
109109
--------------------------
110110

111+
- Improved :program:`clang-tidy-diff.py` script. Add the `-only-check-in-db`
112+
option to exclude files not present in the compilation database, avoiding
113+
false-negative results.
114+
111115
- Improved :program:`run-clang-tidy.py` script. Fixed minor shutdown noise
112116
happening on certain platforms when interrupting the script.
113117

clang/bindings/python/clang/cindex.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2133,6 +2133,14 @@ def get_field_offsetof(self):
21332133
"""Returns the offsetof the FIELD_DECL pointed by this Cursor."""
21342134
return conf.lib.clang_Cursor_getOffsetOfField(self) # type: ignore [no-any-return]
21352135

2136+
def get_base_offsetof(self, parent):
2137+
"""Returns the offsetof the CXX_BASE_SPECIFIER pointed by this Cursor."""
2138+
return conf.lib.clang_getOffsetOfBase(parent, self) # type: ignore [no-any-return]
2139+
2140+
def is_virtual_base(self):
2141+
"""Returns whether the CXX_BASE_SPECIFIER pointed by this Cursor is virtual."""
2142+
return conf.lib.clang_isVirtualBase(self) # type: ignore [no-any-return]
2143+
21362144
def is_anonymous(self):
21372145
"""
21382146
Check whether this is a record type without a name, or a field where
@@ -2687,6 +2695,21 @@ def visitor(field, children):
26872695
conf.lib.clang_Type_visitFields(self, fields_visit_callback(visitor), fields)
26882696
return iter(fields)
26892697

2698+
def get_bases(self):
2699+
"""Return an iterator for accessing the base classes of this type."""
2700+
2701+
def visitor(base, children):
2702+
assert base != conf.lib.clang_getNullCursor()
2703+
2704+
# Create reference to TU so it isn't GC'd before Cursor.
2705+
base._tu = self._tu
2706+
bases.append(base)
2707+
return 1 # continue
2708+
2709+
bases: list[Cursor] = []
2710+
conf.lib.clang_visitCXXBaseClasses(self, fields_visit_callback(visitor), bases)
2711+
return iter(bases)
2712+
26902713
def get_exception_specification_kind(self):
26912714
"""
26922715
Return the kind of the exception specification; a value from
@@ -3940,6 +3963,7 @@ def set_property(self, property, value):
39403963
("clang_getNumDiagnosticsInSet", [c_object_p], c_uint),
39413964
("clang_getNumElements", [Type], c_longlong),
39423965
("clang_getNumOverloadedDecls", [Cursor], c_uint),
3966+
("clang_getOffsetOfBase", [Cursor, Cursor], c_longlong),
39433967
("clang_getOverloadedDecl", [Cursor, c_uint], Cursor),
39443968
("clang_getPointeeType", [Type], Type),
39453969
("clang_getRange", [SourceLocation, SourceLocation], SourceRange),
@@ -3992,6 +4016,7 @@ def set_property(self, property, value):
39924016
[TranslationUnit, SourceRange, POINTER(POINTER(Token)), POINTER(c_uint)],
39934017
),
39944018
("clang_visitChildren", [Cursor, cursor_visit_callback, py_object], c_uint),
4019+
("clang_visitCXXBaseClasses", [Type, fields_visit_callback, py_object], c_uint),
39954020
("clang_Cursor_getNumArguments", [Cursor], c_int),
39964021
("clang_Cursor_getArgument", [Cursor, c_uint], Cursor),
39974022
("clang_Cursor_getNumTemplateArguments", [Cursor], c_int),

clang/bindings/python/tests/cindex/test_type.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,3 +534,28 @@ def test_pretty(self):
534534
self.assertEqual(f.type.get_canonical().pretty_printed(pp), "X")
535535
pp.set_property(PrintingPolicyProperty.SuppressTagKeyword, False)
536536
self.assertEqual(f.type.get_canonical().pretty_printed(pp), "struct X")
537+
538+
def test_base_classes(self):
539+
source = """
540+
class A { int a; };
541+
class B { int b; };
542+
class C { int c; };
543+
template <typename T>
544+
class Template : public A, public B, virtual C {
545+
};
546+
Template<int> instance;
547+
int bar;
548+
"""
549+
tu = get_tu(source, lang="cpp", flags=["--target=x86_64-linux-gnu"])
550+
cursor = get_cursor(tu, "instance")
551+
cursor_type = cursor.type
552+
cursor_type_decl = cursor_type.get_declaration()
553+
self.assertEqual(cursor.kind, CursorKind.VAR_DECL)
554+
bases = list(cursor_type.get_bases())
555+
self.assertEqual(len(bases), 3)
556+
self.assertFalse(bases[0].is_virtual_base())
557+
self.assertEqual(bases[0].get_base_offsetof(cursor_type_decl), 64)
558+
self.assertFalse(bases[1].is_virtual_base())
559+
self.assertEqual(bases[1].get_base_offsetof(cursor_type_decl), 96)
560+
self.assertTrue(bases[2].is_virtual_base())
561+
self.assertEqual(bases[2].get_base_offsetof(cursor_type_decl), 128)

clang/docs/ReleaseNotes.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,6 +1258,10 @@ libclang
12581258
whether the first one comes strictly before the second in the source code.
12591259
- Add ``clang_getTypePrettyPrinted``. It allows controlling the PrintingPolicy used
12601260
to pretty-print a type.
1261+
- Added ``clang_visitCXXBaseClasses``, which allows visiting the base classes
1262+
of a class.
1263+
- Added ``clang_getOffsetOfBase``, which allows computing the offset of a base
1264+
class in a class's layout.
12611265

12621266
Static Analyzer
12631267
---------------
@@ -1405,6 +1409,12 @@ Python Binding Changes
14051409
declaration is an anonymous union or anonymous struct.
14061410
- Added ``Type.pretty_printed`, a binding for ``clang_getTypePrettyPrinted``,
14071411
which allows changing the formatting of pretty-printed types.
1412+
- Added ``Cursor.is_virtual_base``, a binding for ``clang_isVirtualBase``,
1413+
which checks whether a base class is virtual.
1414+
- Added ``Type.get_bases``, a binding for ``clang_visitCXXBaseClasses``, which
1415+
allows visiting the base classes of a class.
1416+
- Added ``Cursor.get_base_offsetof``, a binding for ``clang_getOffsetOfBase``,
1417+
which allows computing the offset of a base class in a class's layout.
14081418

14091419
OpenMP Support
14101420
--------------

clang/include/clang-c/Index.h

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3605,8 +3605,8 @@ CINDEX_LINKAGE enum CXTypeNullabilityKind clang_Type_getNullability(CXType T);
36053605

36063606
/**
36073607
* List the possible error codes for \c clang_Type_getSizeOf,
3608-
* \c clang_Type_getAlignOf, \c clang_Type_getOffsetOf and
3609-
* \c clang_Cursor_getOffsetOf.
3608+
* \c clang_Type_getAlignOf, \c clang_Type_getOffsetOf,
3609+
* \c clang_Cursor_getOffsetOf, and \c clang_getOffsetOfBase.
36103610
*
36113611
* A value of this enumeration type can be returned if the target type is not
36123612
* a valid argument to sizeof, alignof or offsetof.
@@ -3771,6 +3771,15 @@ CINDEX_LINKAGE enum CXRefQualifierKind clang_Type_getCXXRefQualifier(CXType T);
37713771
*/
37723772
CINDEX_LINKAGE unsigned clang_isVirtualBase(CXCursor);
37733773

3774+
/**
3775+
* Returns the offset in bits of a CX_CXXBaseSpecifier relative to the parent
3776+
* class.
3777+
*
3778+
* Returns a small negative number if the offset cannot be computed. See
3779+
* CXTypeLayoutError for error codes.
3780+
*/
3781+
CINDEX_LINKAGE long long clang_getOffsetOfBase(CXCursor Parent, CXCursor Base);
3782+
37743783
/**
37753784
* Represents the C++ access control level to a base class for a
37763785
* cursor with kind CX_CXXBaseSpecifier.
@@ -6648,6 +6657,29 @@ typedef enum CXVisitorResult (*CXFieldVisitor)(CXCursor C,
66486657
CINDEX_LINKAGE unsigned clang_Type_visitFields(CXType T, CXFieldVisitor visitor,
66496658
CXClientData client_data);
66506659

6660+
/**
6661+
* Visit the base classes of a type.
6662+
*
6663+
* This function visits all the direct base classes of a the given cursor,
6664+
* invoking the given \p visitor function with the cursors of each
6665+
* visited base. The traversal may be ended prematurely, if
6666+
* the visitor returns \c CXFieldVisit_Break.
6667+
*
6668+
* \param T the record type whose field may be visited.
6669+
*
6670+
* \param visitor the visitor function that will be invoked for each
6671+
* field of \p T.
6672+
*
6673+
* \param client_data pointer data supplied by the client, which will
6674+
* be passed to the visitor each time it is invoked.
6675+
*
6676+
* \returns a non-zero value if the traversal was terminated
6677+
* prematurely by the visitor returning \c CXFieldVisit_Break.
6678+
*/
6679+
CINDEX_LINKAGE unsigned clang_visitCXXBaseClasses(CXType T,
6680+
CXFieldVisitor visitor,
6681+
CXClientData client_data);
6682+
66516683
/**
66526684
* Describes the kind of binary operators.
66536685
*/

clang/lib/AST/ByteCode/Disasm.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,10 +368,10 @@ LLVM_DUMP_METHOD void EvaluationResult::dump() const {
368368
case LValue: {
369369
assert(Source);
370370
QualType SourceType;
371-
if (const auto *D = Source.dyn_cast<const Decl *>()) {
371+
if (const auto *D = dyn_cast<const Decl *>(Source)) {
372372
if (const auto *VD = dyn_cast<ValueDecl>(D))
373373
SourceType = VD->getType();
374-
} else if (const auto *E = Source.dyn_cast<const Expr *>()) {
374+
} else if (const auto *E = dyn_cast<const Expr *>(Source)) {
375375
SourceType = E->getType();
376376
}
377377

clang/lib/AST/ExternalASTMerger.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,8 @@ bool ExternalASTMerger::HasImporterForOrigin(ASTContext &OriginContext) {
276276
template <typename CallbackType>
277277
void ExternalASTMerger::ForEachMatchingDC(const DeclContext *DC,
278278
CallbackType Callback) {
279-
if (Origins.count(DC)) {
280-
ExternalASTMerger::DCOrigin Origin = Origins[DC];
279+
if (auto It = Origins.find(DC); It != Origins.end()) {
280+
ExternalASTMerger::DCOrigin Origin = It->second;
281281
LazyASTImporter &Importer = LazyImporterForOrigin(*this, *Origin.AST);
282282
Callback(Importer, Importer.GetReverse(), Origin.DC);
283283
} else {

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4889,6 +4889,9 @@ llvm::DILocalVariable *CGDebugInfo::EmitDeclare(const VarDecl *VD,
48894889
if (IPD->getParameterKind() == ImplicitParamKind::CXXThis ||
48904890
IPD->getParameterKind() == ImplicitParamKind::ObjCSelf)
48914891
Flags |= llvm::DINode::FlagObjectPointer;
4892+
} else if (const auto *PVD = dyn_cast<ParmVarDecl>(VD)) {
4893+
if (PVD->isExplicitObjectParameter())
4894+
Flags |= llvm::DINode::FlagObjectPointer;
48924895
}
48934896

48944897
// Note: Older versions of clang used to emit byval references with an extra

clang/lib/Sema/SemaCodeComplete.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ class ResultBuilder {
121121
return;
122122
}
123123

124-
if (const NamedDecl *PrevND =
125-
DeclOrVector.dyn_cast<const NamedDecl *>()) {
124+
if (const NamedDecl *PrevND = dyn_cast<const NamedDecl *>(DeclOrVector)) {
126125
// 1 -> 2 elements: create the vector of results and push in the
127126
// existing declaration.
128127
DeclIndexPairVector *Vec = new DeclIndexPairVector;
@@ -702,7 +701,7 @@ ResultBuilder::ShadowMapEntry::begin() const {
702701
if (DeclOrVector.isNull())
703702
return iterator();
704703

705-
if (const NamedDecl *ND = DeclOrVector.dyn_cast<const NamedDecl *>())
704+
if (const NamedDecl *ND = dyn_cast<const NamedDecl *>(DeclOrVector))
706705
return iterator(ND, SingleDeclIndex);
707706

708707
return iterator(cast<DeclIndexPairVector *>(DeclOrVector)->begin());

0 commit comments

Comments
 (0)