在Clang的措辞中,什么是"annonated fallthrough"和"部分注释方法"?

use*_*672 5 c c++ clang

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

def warn_unannotated_fallthrough : Warning<
  "unannotated fall-through between switch labels">,
  InGroup<ImplicitFallthrough>, DefaultIgnore;
Run Code Online (Sandbox Code Playgroud)

def warn_unannotated_fallthrough_per_function : Warning<
  "unannotated fall-through between switch labels in partly-annotated "
  "function">, InGroup<ImplicitFallthroughPerFunction>, DefaultIgnore;
Run Code Online (Sandbox Code Playgroud)

我试图搜索这些警告的提及,并发现此代码snipplet:

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    ;
       } 
Run Code Online (Sandbox Code Playgroud)

clang的意思是"注释"?

Mal*_*mir 6

顺便说一句,从 C++17 标准属性[[fallthrough]]可用于指示当代码打算失败时这不是警告。在检查您的 switch-case 的逻辑错误后,在不break使用新属性的情况下结束 case :

#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;
    }
}
Run Code Online (Sandbox Code Playgroud)


Jam*_*nze 4

在这种情况下,“注释”可能指的是编译器会识别的一些特殊注释。例如,对于“未注释的失败”(如您的代码片段中所示),代码位:

case 0:
    n += 100;
case 1:
    //  ...
Run Code Online (Sandbox Code Playgroud)

通常是一个错误,因为程序员忘记了break. 所以编译器会发出警告。在一些罕见的情况下(例如达夫的设备),缺少中断是故意的;“注释”是一种告诉编译器(和其他阅读代码的人)这是有意为之的方式,并且不要发出警告。

从您的示例片段中,我了解到 clang 使用新的 C++11 属性语法,而不是传统的特殊注释。(这里的属性就是[[clang::fallthrough]]; 语句。)

从您的代码片段来看,我认为如果函数不包含属性,则使用第一条消息(大多数不会,因为这是一个新的 C++11 功能),如果包含属性,则将使用第二条消息。(从用户的角度来看:如果正在使用属性,如果丢失的中断是故意的,人们会期望它们。如果它们不是故意的,那么它们不存在于丢失的中断上这一事实并不能告诉您这一点这不是故意的;你必须仔细观察。)

将错误消息翻译成另一种语言可能很棘手,因为它取决于新的 C++11 功能的可接受术语;由于它是一项新功能,因此可能没有既定术语。另外值得注意的是,clang 使用“注释”,尽管标准从未使用术语“注释”或“注释”。从上下文和您的示例片段来看,很明显“带注释”意味着“具有特定形式的 C++11 属性”,但除此之外,您可能需要猜测一下(或在论坛中询问)目标语言:例如,过去,fr.comp.lang.c++ 对于法语非常有用)。