Clang的措辞中"annotated fallthrough"和"partly annotated method"是什么?

What are "annotated fallthrough" and "partly annotated method" in Clang's wording?

本文关键字:annotated partly method 是什么 fallthrough Clang      更新时间:2023-10-16

我正在将Clang错误消息翻译成另一种语言,在文件底部附近,我发现了以下条目:

def warn_unannotated_fallthrough : Warning<
"unannotated fall-through between switch labels">,
InGroup<ImplicitFallthrough>, DefaultIgnore;

def warn_unannotated_fallthrough_per_function : Warning<
"unannotated fall-through between switch labels in partly-annotated "
"function">, InGroup<ImplicitFallthroughPerFunction>, DefaultIgnore;

我试图搜索这些警告的提及,并找到了以下代码截图:

int fallthrough(int n) {
switch (n / 10) {
case 0:
n += 100;
-    case 1:  // expected-warning{{unannotated fall-through between switch labels in partly annotated method}} expected-note{{insert '[[clang::fallthrough]];' to silence this warning}} expected-note{{insert 'break;' to avoid fall-through}}
+    case 1:  // expected-warning{{unannotated fall-through}} expected-note{{insert '[[clang::fallthrough]];' to silence this warning}} expected-note{{insert 'break;' to avoid fall-through}}
switch (n) {
case 111:
n += 111;
[[clang::fallthrough]];
case 112:
n += 112;
-      case 113:  // expected-warning{{unannotated fall-through between switch labels in partly annotated method}} expected-note{{insert '[[clang::fallthrough]];' to silence this warning}} expected-note{{insert 'break;' to avoid fall-through}}
+      case 113:  // expected-warning{{unannotated fall-through}} expected-note{{insert '[[clang::fallthrough]];' to silence this warning}} expected-note{{insert 'break;' to avoid fall-through}}
n += 113;
break    ;
} 

Clang所说的"注释"是什么意思?

顺便说一下,从 C++17 开始,标准属性[[fallthrough]]可用于指示当代码要失败时不是警告。 在检查您的开关案例是否存在逻辑错误后,案例在没有break的情况下结束,只需使用 new 属性:

#include <iostream>
enum class Layers {
Undefined, Back, Middle, Front
};
int main() {
Layers layer{ Layers::Undefined };
// ...
switch (layer)
{
case Layers::Back:
std::cout << "Back layer processed" << std::endl;
break;
case Layers::Middle:
std::cout << "Middle layer partially processed" << std::endl;
[[fallthrough]]; //(dont forget the semicolon) Suppressed warning
case Layers::Front:
std::cout << "And some code for middle and front layers" << std::endl;
break;
case Layers::Undefined:
std::cout << "Undefined layer" << std::endl;
}
}

在这种情况下,"注释"可能是指一些特殊的 注释,编译器将识别。 对于 例如,"未注释的落通"(如在您的代码中 片段),代码位:

case 0:
n += 100;
case 1:
//  ...

通常是由于程序员忘记break而出现的错误。 因此,编译器将发出警告。 在一些极少数情况下(达夫的 设备,例如),缺失的中断是故意的;这 "注释"是一种告诉编译器(和其他阅读代码的人)它是 有意,并且不发出警告。

从您的示例片段中,我发现 clang 正在使用新的 C++11属性语法,而不是传统的特殊 评论。 (此处的属性是[[clang::fallthrough]];声明。

从你的片段来看,我收集到第一条消息是 如果函数不包含属性(并且大多数不会, 由于这是一个新的C++11功能),并且将使用第二个功能 如果是这样。 (从用户的角度来看:如果属性是 使用,如果缺少的中断是 故意。 如果不是,那么他们不是 在缺失的休息时间里存在并不能告诉你它不是 故意;你必须仔细观察。

将错误消息翻译成另一种语言可能是 棘手,因为它取决于新C++11的接受术语 特征;由于这是一项新功能,因此可能没有 既定期限。 另外有趣的是,叮当使用 "注释",尽管标准从未使用过该术语 "注释"或"注释"。 从上下文和您的示例 片段,很明显"注释"意味着"具有 C++11 个属性 特定形式",但除此之外,您可能会 必须猜测一下(或在目标语言的论坛中提问: 过去,fr.comp.lang.c++对法语非常好,因为 示例)。

在这种情况下,"注释"告诉编译器您打算跳过开关大小写的中断。这样编译器就会向您展示您可能忘记的地方break.然后,您可以再次检查它并确认是否有意。