Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix -Wpotentially-evaluated-expression warnings #9355

Merged
merged 1 commit into from Jun 8, 2015

Conversation

davidlt
Copy link
Contributor

@davidlt davidlt commented May 30, 2015

Clang warns if expression inside typeid() or sizeof() could create
unwanted side effects.

Two warnings were in this package. Full message:

warning: expression with side effects will be evaluated despite being
used as an operand to 'typeid' [-Wpotentially-evaluated-expression]

I couldn't find code in CMSSW using getAs<>(). They are only tested in
private tests (different package), but it's not executed in CMSSW
testsuite. In addition to that test input file is not present.

The pieces generating message were tested manually in a standalone
testcase.

Tested on GCC (4.9.1) and Clang (pre-3.6).

If it's impossible to demangle symbols at run-time, message will be:

You can't convert a 'N3edm3PtrI10Candidate2EE' to a '9Candidate'
Note: you can use 'c++filt -t' command to convert the above in human readable types.

This is a light update to old one. Also mention -t option, as c++filt
does not demangle type symbols by default.

If we can demangle symbols at run-time, message will be:

You can't convert a 'Candidate2' to a 'Candidate'

Notice that instead of printing edm::Ptr<Candidate> it talks about
actual type in edm::Ptr<>.

Signed-off-by: David Abdurachmanov David.Abdurachmanov@cern.ch

Clang warns if expression inside typeid() or sizeof() could create
unwanted side effects.

Two warnings were in this package. Full message:

    warning: expression with side effects will be evaluated despite being
    used as an operand to 'typeid' [-Wpotentially-evaluated-expression]

I couldn't find code in CMSSW using getAs<>(). They are only tested in
private tests (different package), but it's not executed in CMSSW
testsuite. In addition to that test input file is not present.

The pieces generating message were tested manually in a standalone
testcase.

Tested on GCC (4.9.1) and Clang (pre-3.6).

If it's impossible to demangle symbols at run-time, message will be:

    You can't convert a 'N3edm3PtrI10Candidate2EE' to a '9Candidate'
    Note: you can use 'c++filt -t' command to convert the above in human readable types.

This is a light update to old one. Also mention '-t' option, as c++filt
does not demangle type symbols by default.

If we can demangle symbols at run-time, message will be:

    You can't convert a 'Candidate2' to a 'Candidate'

Notice that instead of printing 'edm::Ptr<Candidate>' it talks about
actual type in 'edm::Ptr<>'.

Signed-off-by: David Abdurachmanov <David.Abdurachmanov@cern.ch>
@cmsbuild
Copy link
Contributor

A new Pull Request was created by @davidlt for CMSSW_7_5_X.

Fix -Wpotentially-evaluated-expression warnings

It involves the following packages:

DataFormats/PatCandidates

@cmsbuild, @vadler, @monttj can you please review it and eventually sign? Thanks.
@gpetruc this is something you requested to watch as well.
You can sign-off by replying to this message having '+1' in the first line of your reply.
You can reject by replying to this message having '-1' in the first line of your reply.
If you are a L2 or a release manager you can ask for tests by saying 'please test' in the first line of a comment.
@Degano you are the release manager for this.
You can merge this pull request by typing 'merge' in the first line of your comment.

@davidlt
Copy link
Contributor Author

davidlt commented May 30, 2015

Again only users of this in CMSSW are:

PhysicsTools/PatAlgos/test/private/TestEventHypothesisReader.cc
PhysicsTools/PatAlgos/test/private/TestEventHypothesisWriter.cc

They are private and not executed in CMSSW test suite.

For testing of message I was using this standalone test (for the record):

#include <typeinfo>
#include <iostream>
#include <sstream>
#include <type_traits>
#include <cxxabi.h>

struct Candidate {
};

namespace edm {
  template <class T>
  struct Ptr {
    typedef T value_type;
  };
}

namespace reco {
  typedef edm::Ptr<Candidate> CandidatePtr;
}

typedef reco::CandidatePtr CandRefType;


char *
getDemangledSymbol(const char* mangled_sym) {
  int status;
  char *demangled_sym = demangled_sym = abi::__cxa_demangle(mangled_sym, nullptr, nullptr, &status);
  return (status == 0) ? demangled_sym : nullptr;
}

template<typename T>
std::string
createExceptionMessage(const CandRefType &ref) {
  std::stringstream exc_msg;
  char *current_type = getDemangledSymbol(typeid(std::remove_reference<decltype(ref)>::type::value_type).name());
  char *target_type = getDemangledSymbol(typeid(T).name());
  if (current_type == nullptr && target_type != nullptr) {
    exc_msg << "You can't convert a '" << current_type << "' to a '" << target_type << "'" << std::endl;
    free(current_type);
    free(target_type);
  } else {
    exc_msg << "You can't convert a '" << typeid(ref).name() << "' to a '" << typeid(T).name() << "'" << std::endl;
    exc_msg << "Note: you can use 'c++filt -t' command to convert the above in human readable types." << std::endl;
  }
  return exc_msg.str();
}

int main(void) {
  CandRefType tmp;
  std::cout << createExceptionMessage<Candidate>(tmp) << std::endl;
  return 0;
}

@cmsbuild
Copy link
Contributor

@cmsbuild
Copy link
Contributor

@monttj
Copy link
Contributor

monttj commented Jun 8, 2015

+1

@cmsbuild
Copy link
Contributor

cmsbuild commented Jun 8, 2015

This pull request is fully signed and it will be integrated in one of the next CMSSW_7_5_X IBs unless changes (tests are also fine). This pull request requires discussion in the ORP meeting before it's merged. @davidlange6, @Degano, @smuzaffar

@davidlange6
Copy link
Contributor

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants