12

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"?

3 Answers 3

11

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;
    }
}
1
  • Thanks for the "dont forget the semicolon"...
    – Liam
    Jun 23 at 14:10
4

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).

2

"Annotating" in this case is telling compiler that you intented to skip break in switch case. This way compiler is showing you places that you maybe forgot about break. You can then check it out again and confirm if that was intented.

4
  • I think I got that. What might an "annotated function" be then?
    – Einheri
    Jan 22, 2014 at 9:26
  • @user3109672 This page might help.
    – unwind
    Jan 22, 2014 at 9:48
  • @unwind That page seems to explain a pre-standard annotation used by g++ (as an extension). The code snippet suggests a use of C++11 attribute declarations. (See §7.6 in the C++11 standard.) Jan 22, 2014 at 10:04
  • I just noticed the unexpected syntax in the code snippet; I had to look it up. The principle is the same, at any rate, and maybe the older g++ versions do speak of "annotating", and if they've been translated, he could find the word there. Jan 22, 2014 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.