我試圖理解下面的代碼。但我不明白。基本上,下面的代碼當前檢查 ac 或 cpp 檔案中的 if 條件。
if ($perl_version_ok &&
$line =~ /^\ (.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/) {
# Throw error
}
其中 Constant 是任何宏或任何常量值;LvalOrFunc 是任何變數或函式呼叫;比較是 !=、==、&& 等操作
if 檢查這樣的代碼if(CONST_VALUE == x),其中 CONST_VALUE 是一些宏。在這種情況下,它是真的并且進入 if 條件。
但我想檢查相反的if(x == CONST_VALUE ),然后拋出錯誤。
請幫助理解這條線以及如何達到預期的結果。
注意: 代碼來自 linux 內核目錄,可在此處獲得:https ://github.com/torvalds/linux/blob/master/scripts/checkpatch.pl
代碼行號:5483
uj5u.com熱心網友回復:
該代碼不檢查if(CONST_VALUE == x). 如源代碼行上方的注釋所示
# comparisons with a constant or upper case identifier on the left
# avoid cases like "foo BAR < baz"
# only fix matches surrounded by parentheses to avoid incorrect
# conversions like "FOO < baz() 5" being "misfixed" to "baz() > FOO 5"
它檢查加號后跟一個CONSTANT_VALUE == x. 正\ 則運算式中的 匹配加號。
$line =~ /^\ (.*)\b($Constant|[A-Z_][A-Z0-9_]*)\s*($Compare)\s*($LvalOrFunc)/
^ ^ ^ ^ ^
| | | | |
binding | | | word
operator | | | boundary
start | |
of string | |
plus |
anything
恢復比較值應該很容易:
($LvalOrFunc)\s*($Compare)\s*($Constant|[A-Z_][A-Z0-9_]*)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/425543.html
