Passing #pragma to clang

Hi all,

I am running the following code which spits out “[-Wunused-value]” warnings,

bool setNameGeneral(TObject* obj, const std::string& name) {
    if (obj != 0) {
        TClass* kl = obj->IsA();
        TMethod* klm = kl->GetMethod("SetName", "\"Reference\"");
        if (klm) {
            obj->Execute("SetName", ("\""+name+"\"").c_str());
            return true;
        }
    }
    return false;
}
...
setNameGeneral(obj, somename);

E.g.

In file included from input_line_1:1:
In file included from /afs/cern.ch/sw/lcg/releases/gcc/4.8.1/x86_64-slc6/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.8.1/../../../../include/c++/4.8.1/new:14:
input_line_33319:2:2: warning: expression result unused [-Wunused-value]
 "Rphi";
 ^~~~~~
In file included from input_line_1:1:
In file included from /afs/cern.ch/sw/lcg/releases/gcc/4.8.1/x86_64-slc6/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.8.1/../../../../include/c++/4.8.1/new:14:
input_line_33320:2:2: warning: expression result unused [-Wunused-value]
 "charge";
 ^~~~~~~~
In file included from input_line_1:1:
In file included from /afs/cern.ch/sw/lcg/releases/gcc/4.8.1/x86_64-slc6/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.8.1/../../../../include/c++/4.8.1/new:14:
input_line_33321:2:2: warning: expression result unused [-Wunused-value]
 "d0";
 ^~~~
In file included from input_line_1:1:
In file included from /afs/cern.ch/sw/lcg/releases/gcc/4.8.1/x86_64-slc6/bin/../lib/gcc/x86_64-unknown-linux-gnu/4.8.1/../../../../include/c++/4.8.1/new:14:
input_line_33322:2:2: warning: expression result unused [-Wunused-value]
 "d0sig";
 ^~~~~~~

I think these warnings can be disabled if setting pragma clang statement,

#pragma clang diagnostic ignored "-Wunused-value"

Do you know how to do that? Or maybe there is a better way to get rid of these nasty clang warnings?

Thanks!

Hi,

it is not clear to me how you are executing the code but this can be a start

// This is macro test.C
// ---------------------------
// Uncomment to silence the warning.
// #pragma clang diagnostic ignored "-Wunused-result"

int foo() { return 3; }
int bar() __attribute__((warn_unused_result));
int bar() { return 5; }

int test(){
    int i = foo();
    bar();    /* line 9 */
    double j = i *4;
    return 0;
}

In any case I would recommend to properly fix the warnings rather than suppressing them whenever possible.

Cheers,
Danilo

Hi Danilo,

The code basically gets executed here,

obj->Execute("SetName", ("\""+name+"\"").c_str());

and this is where warnings come from. I cannot apply directly your method since I am not running script as such, I only get objects as TObject instances. On these instances I try executing “SetName” in clang.

But how can I pass #pragma statement to clang in this case is not clear to me (there is no script to be executed, it’s just a method).

Regards,
Yuriy

Hi Yuriy,

can you share a minimal reproducer?

Danilo

Hi Danilo,

Sorry, it took me some time to prepare a minimal reproducer. I have also submitted a JIRA ticket to the ROOT authors - https://sft.its.cern.ch/jira/browse/ROOT-8072

#include "TH1F.h"
#include "TObject.h"

int main(int argc, char* argv[]){

    TH1F* histo = new TH1F("myhisto", "myhisto", 100, 0, 100);
    TObject* obj = dynamic_cast<TObject*>(histo);
    std::string name = "myhisto";
    for (int i=0; i<33000; i++) {
        obj->Execute("SetName", ("\""+name+"\"").c_str());
    }
    return 0;
}

Thank you for the help!
Regards,
Yuriy