Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c

clang

I'm translating Clang error messages into another language, and near the bottom of the file I found the following entries:

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

and

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

I attempted to search for mentions of these warnings, and found this code 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    ;
       } 

What does Clang mean by "annotated"?

like image 668
Einheri Avatar asked Jan 22 '14 09:01

Einheri


2 Answers

By the way, from C++17 standard attribute [[fallthrough]] is available to indicate that isn't a warning when the code is meant to fall through. After checking your switch-case on logical mistakes in place where a case ended without break just use new attribute:

#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;
    }
}
like image 121
Malov Vladimir Avatar answered Sep 19 '22 14:09

Malov Vladimir


In this case, "annotated" probably refers to some special comments, that the compiler will recognize. For the "unannotated fall-through", for example (as in your code snippet), the bit of code:

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

is usually an error, due to the programmer forgetting a break. So the compiler will emit a warning. In some rare cases (Duff's device, for example), the missing break is intentional; the "annotation" is a way of telling the compiler (and other people reading the code) that it is intentional, and to not emit the warning.

From your example snippet, I gather that clang is using the new C++11 attribute syntax, rather than the traditional special comments. (The attribute here is the [[clang::fallthrough]]; statement.)

Judging from your snippet, I gather that the first message is used if the function contains no attributes (and most won't, since this is a new C++11 feature), and the second will be used if it does. (From a user point of view: if attributes are being used, one would expect them if the missing break was intentional. If they're not, then the fact that they aren't present on a missing break doesn't tell you that it wasn't intentional; you have to look closer.)

Translating the error messages into another language is probably tricky, since it depends on the accepted term for the new C++11 feature; since it's a new feature, there may not be an established term. Also it's interesting to note that clang uses "annotated", although the standard never uses the term "annotate" or "annotation". From context, and your example snippet, it's clear that "annotated" means "has C++11 attributes of a particular form", but beyond that, you're probably going to have to guess a bit (or ask in a forum in the target language: in the past, fr.comp.lang.c++ was very good for French, for example).

like image 31
James Kanze Avatar answered Sep 20 '22 14:09

James Kanze