Skip to content

Commit 396dd44

Browse files
committed
fix clang tidy warnings about pointer conversion
Signed-off-by: Ethan Mahintorabi <[email protected]>
1 parent 0aa115c commit 396dd44

File tree

1 file changed

+28
-17
lines changed

1 file changed

+28
-17
lines changed

src/dbSta/src/dbNetwork.cc

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,11 @@ ObjectId dbNetwork::getDbNwkObjectId(const dbObject* object) const
165165
return 0;
166166
}
167167

168-
// We have exactly 8 values avalible in the lower 3 bits of the
168+
// We have exactly 8 values available in the lower 3 bits of the
169169
// pointer due to these classes all having 8 byte alignment. Used
170170
// to avoid having to call into the database to figure out the
171171
// type information, which requires a lot of pointer indirection.
172172

173-
// Used to clear the pointer tag.
174-
static constexpr std::uintptr_t kPointerClearTag = ~(std::uintptr_t(0b111U));
175173
// Used to get the value of the pointer tag.
176174
static constexpr std::uintptr_t kPointerTagMask = std::uintptr_t(0b111U);
177175

@@ -1230,10 +1228,13 @@ ObjectId dbNetwork::id(const Pin* pin) const
12301228

12311229
if (hierarchy_) {
12321230
// get the id for hierarchical objects using dbid.
1233-
std::uintptr_t pointer_without_tag
1234-
= reinterpret_cast<std::uintptr_t>(pin) & kPointerClearTag;
1231+
std::uintptr_t tag_value
1232+
= reinterpret_cast<std::uintptr_t>(pin) & kPointerTagMask;
1233+
// Need to cast to char pin to avoid compiler error for pointer
1234+
// arithmetic on incomplete types
1235+
const char* char_pin = reinterpret_cast<const char*>(pin);
12351236
const dbObject* obj
1236-
= reinterpret_cast<const dbObject*>(pointer_without_tag);
1237+
= reinterpret_cast<const dbObject*>(char_pin - tag_value);
12371238
return getDbNwkObjectId(obj);
12381239
}
12391240
if (iterm != nullptr) {
@@ -2786,10 +2787,18 @@ void dbNetwork::staToDb(const Pin* pin,
27862787
iterm = nullptr;
27872788
bterm = nullptr;
27882789
moditerm = nullptr;
2790+
2791+
// Get the value of the tag
27892792
std::uintptr_t pointer_with_tag = reinterpret_cast<std::uintptr_t>(pin);
27902793
PinPointerTags tag_value
27912794
= static_cast<PinPointerTags>(pointer_with_tag & kPointerTagMask);
2792-
std::uintptr_t pointer_without_tag = pointer_with_tag & kPointerClearTag;
2795+
2796+
// Cast to char* and avoid casting an integral type to pointer type
2797+
// by doing pointer arithmetic. Compiler apparently prefer this sytle.
2798+
const char* char_pointer_pin = reinterpret_cast<const char*>(pin);
2799+
char* pointer_without_tag = const_cast<char*>(
2800+
char_pointer_pin - static_cast<std::uintptr_t>(tag_value));
2801+
27932802
if (pointer_without_tag) {
27942803
switch (tag_value) {
27952804
case PinPointerTags::kDbIterm:
@@ -2966,10 +2975,10 @@ Instance* dbNetwork::dbToSta(dbModInst* inst) const
29662975

29672976
Pin* dbNetwork::dbToSta(dbModITerm* mod_iterm) const
29682977
{
2969-
auto mod_iterm_tagged_ptr = reinterpret_cast<std::uintptr_t>(mod_iterm);
2970-
mod_iterm_tagged_ptr
2971-
|= static_cast<std::uintptr_t>(PinPointerTags::kDbModIterm);
2972-
return reinterpret_cast<Pin*>(mod_iterm_tagged_ptr);
2978+
char* unaligned_pointer = reinterpret_cast<char*>(mod_iterm);
2979+
return reinterpret_cast<Pin*>(
2980+
unaligned_pointer
2981+
+ static_cast<std::uintptr_t>(PinPointerTags::kDbModIterm));
29732982
}
29742983

29752984
Net* dbNetwork::dbToSta(dbModNet* net) const
@@ -3014,16 +3023,18 @@ const Net* dbNetwork::dbToSta(const dbModNet* net) const
30143023

30153024
Pin* dbNetwork::dbToSta(dbBTerm* bterm) const
30163025
{
3017-
auto bterm_tagged_ptr = reinterpret_cast<std::uintptr_t>(bterm);
3018-
bterm_tagged_ptr |= static_cast<std::uintptr_t>(PinPointerTags::kDbBterm);
3019-
return reinterpret_cast<Pin*>(bterm_tagged_ptr);
3026+
char* unaligned_pointer = reinterpret_cast<char*>(bterm);
3027+
return reinterpret_cast<Pin*>(
3028+
unaligned_pointer
3029+
+ static_cast<std::uintptr_t>(PinPointerTags::kDbBterm));
30203030
}
30213031

30223032
Pin* dbNetwork::dbToSta(dbITerm* iterm) const
30233033
{
3024-
auto iterm_tagged_ptr = reinterpret_cast<std::uintptr_t>(iterm);
3025-
iterm_tagged_ptr |= static_cast<std::uintptr_t>(PinPointerTags::kDbIterm);
3026-
return reinterpret_cast<Pin*>(iterm_tagged_ptr);
3034+
char* unaligned_pointer = reinterpret_cast<char*>(iterm);
3035+
return reinterpret_cast<Pin*>(
3036+
unaligned_pointer
3037+
+ static_cast<std::uintptr_t>(PinPointerTags::kDbIterm));
30273038
}
30283039

30293040
Term* dbNetwork::dbToStaTerm(dbBTerm* bterm) const

0 commit comments

Comments
 (0)