好的,我需要扩展代码,但这里有一些非常复杂的东西:
((Traces.Last=new SymbolSequence::Node)->Prev=(Discard.First=Processing1)->Prev)->Next=Traces.Last;
我需要将这些分配拆分为语句,但我不知道这个顺序。带括号的顺序在这里让我感到困惑,是像数学还是编译器做他想做的事?
非常感谢。
该代码可能旨在
Traces.Last = new SymbolSequence::Node;
Discard.First = Processing1;
Traces.Last->Prev = Discard.First->Prev;
Traces.Last->Prev->Next = Traces.Last;
但是,由于它既访问 Traces.Last 又在没有中间序列点的情况下设置它,实际结果是未定义的。编译器很可能会产生类似的东西:
temp = Traces.Last;
Traces.Last = new SymbolSequence::Node;
Discard.First = Processing1;
Traces.Last->Prev = Discard.First->Prev;
Traces.Last->Prev->Next = temp;
并且最初的程序员打算这样做。
我理解你的痛苦。那些编写好像整个世界都依赖于一行代码的开发人员应该在他们的女朋友面前被打湿面条。
当像这样重构代码时,一次开始一段代码总是有用的。并随时测试。
取最里面的括号组中的一个小表达式并将其重构出来。也许通过将它分配给一个变量。每个代码都是不同的,所以你尽你所能。这将一点一点地缩小那一大行代码。继续这样做,直到表达式越来越小。它还有助于在运算符之间放置空白,以便实际查看代码的分组并帮助收集评估顺序。
因此,鉴于您的原始代码:
((Traces.Last=new SymbolSequence::Node)->Prev=(Discard.First=Processing1)->Prev)->Next=Traces.Last;
将其拆分为:
Traces.Last = new SymbolSequence::Node
((Traces.Last)->Prev = (Discard.First = Processing1)->Prev)->Next = Traces.Last;
现在处理另一个内部任务:
Traces.Last = new SymbolSequence::Node
Discard.First = Processing1
((Traces.Last)->Prev = (Discard.First)->Prev)->Next = Traces.Last;
然后我处理下一个最里面的任务
Traces.Last = new SymbolSequence::Node
Discard.First = Processing1
(Traces.Last)->Prev = (Discard.First)->Prev
( (Traces.Last)->Prev )->Next = Traces.Last;
我希望这会有所帮助。
除了评论中描述的未定义行为之外,下面是根据clang++
. AST 显示了运算符在解析时的优先级。
我不得不用一些类型来引导你的例子来满足clang++
:
struct SymbolSequence
{
struct Node
{
Node *First;
Node *Last;
Node *Prev;
Node *Next;
};
};
void foo()
{
SymbolSequence::Node *Processing1;
SymbolSequence::Node Traces;
SymbolSequence::Node Discard;
((Traces.Last=new SymbolSequence::Node)->Prev=(Discard.First=Processing1)->Prev)->Next=Traces.Last;
}
AST:
void foo() (CompoundStmt 0x308b6e0 <go.cpp:14:1, line:19:1>
(DeclStmt 0x3088dc8 <line:15:5, col:38>
(0x3088d70 "SymbolSequence::Node *Processing1"))
(DeclStmt 0x30891d8 <line:16:5, col:32>
(0x3088e40 "SymbolSequence::Node Traces =
(CXXConstructExpr 0x30891a8 <col:26> 'struct SymbolSequence::Node':'struct SymbolSequence::Node''void (void)')"))
(DeclStmt 0x30892d8 <line:17:5, col:33>
(0x3089250 "SymbolSequence::Node Discard =
(CXXConstructExpr 0x30892a8 <col:26> 'struct SymbolSequence::Node':'struct SymbolSequence::Node''void (void)')"))
(BinaryOperator 0x308b6b8 <line:18:5, col:99> 'struct SymbolSequence::Node *' lvalue '='
(MemberExpr 0x308b618 <col:5, col:87> 'struct SymbolSequence::Node *' lvalue ->Next 0x3088b60
(ImplicitCastExpr 0x308b600 <col:5, col:84> 'struct SymbolSequence::Node *' <LValueToRValue>
(ParenExpr 0x308b5e0 <col:5, col:84> 'struct SymbolSequence::Node *' lvalue
(BinaryOperator 0x308b5b8 <col:6, col:80> 'struct SymbolSequence::Node *' lvalue '='
(MemberExpr 0x308b448 <col:6, col:46> 'struct SymbolSequence::Node *' lvalue ->Prev 0x3088b00
(ImplicitCastExpr 0x308b430 <col:6, col:43> 'struct SymbolSequence::Node *' <LValueToRValue>
(ParenExpr 0x308b410 <col:6, col:43> 'struct SymbolSequence::Node *' lvalue
(BinaryOperator 0x308b3e8 <col:7, col:39> 'struct SymbolSequence::Node *' lvalue '='
(MemberExpr 0x3089318 <col:7, col:14> 'struct SymbolSequence::Node *' lvalue .Last 0x3088aa0
(DeclRefExpr 0x30892f0 <col:7> 'struct SymbolSequence::Node':'struct SymbolSequence::Node' lvalue Var 0x3088e40 'Traces' 'struct SymbolSequence::Node':'struct SymbolSequence::Node'))
(CXXNewExpr 0x308b390 <col:19, col:39> 'struct SymbolSequence::Node *'
(CXXConstructExpr 0x308b360 <col:23> 'struct SymbolSequence::Node':'struct SymbolSequence::Node''void (void)'))))))
(ImplicitCastExpr 0x308b5a0 <col:51, col:80> 'struct SymbolSequence::Node *' <LValueToRValue>
(MemberExpr 0x308b570 <col:51, col:80> 'struct SymbolSequence::Node *' lvalue ->Prev 0x3088b00
(ImplicitCastExpr 0x308b558 <col:51, col:77> 'struct SymbolSequence::Node *' <LValueToRValue>
(ParenExpr 0x308b538 <col:51, col:77> 'struct SymbolSequence::Node *' lvalue
(BinaryOperator 0x308b510 <col:52, col:66> 'struct SymbolSequence::Node *' lvalue '='
(MemberExpr 0x308b4a0 <col:52, col:60> 'struct SymbolSequence::Node *' lvalue .First 0x3088a40
(DeclRefExpr 0x308b478 <col:52> 'struct SymbolSequence::Node':'struct SymbolSequence::Node' lvalue Var 0x3089250 'Discard' 'struct SymbolSequence::Node':'struct SymbolSequence::Node'))
(ImplicitCastExpr 0x308b4f8 <col:66> 'struct SymbolSequence::Node *' <LValueToRValue>
(DeclRefExpr 0x308b4d0 <col:66> 'struct SymbolSequence::Node *' lvalue Var 0x3088d70 'Processing1' 'struct SymbolSequence::Node *')))))))))))
(ImplicitCastExpr 0x308b6a0 <col:92, col:99> 'struct SymbolSequence::Node *' <LValueToRValue>
(MemberExpr 0x308b670 <col:92, col:99> 'struct SymbolSequence::Node *' lvalue .Last 0x3088aa0
(DeclRefExpr 0x308b648 <col:92> 'struct SymbolSequence::Node':'struct SymbolSequence::Node' lvalue Var 0x3088e40 'Traces' 'struct SymbolSequence::Node':'struct SymbolSequence::Node')))))