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

Warning for ODR violation #23533

Open
hfinkel opened this issue Apr 8, 2015 · 1 comment
Open

Warning for ODR violation #23533

hfinkel opened this issue Apr 8, 2015 · 1 comment
Labels
bugzilla Issues migrated from bugzilla c++ clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer

Comments

@hfinkel
Copy link
Collaborator

hfinkel commented Apr 8, 2015

Bugzilla Link 23159
Version trunk
OS Linux
CC @DougGregor,@zygoloid

Extended Description

Richard provided to me this example showing ODR violation:

#include

// implicitly internal linkage due to 'const'
const int n = 5;

// odr violation, captures n by reference
inline auto f() { return std::make_pair(n, 3); }

// much more obvious odr violation, takes address of n
inline auto g() {
static const int *p = &n;
return p;
}

Clang compiles this, however, without issuing any warnings. Taking the address of a variable with internal linkage in an inline function is probably not a good idea.

@llvmbot
Copy link
Collaborator

llvmbot commented Oct 5, 2015

There are additional examples of this sort of error that should produce a warning. Probably inline functions and template definitions should warn about any usage of names with internal linkage (and entities in anonymous namespaces, before those were made to have internal linkage).

namespace {
inline void foo() {}
}

static inline void bar() {}

// ODR violations if these appear in a header and that header is included in more than one implementation file:

inline void baz() { foo(); bar(); } 

template<typename T> void qux() { foo(); bar(); }

I think warning about such definitions as soon as they're seen in a header, without waiting to see if that header is included in more than one implementation file, is reasonable.

The fix-it would be to either make these static or in an anonymous namespace, or to move the definitions to an implementation file.

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 9, 2021
@Quuxplusone Quuxplusone added the clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer label Jan 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla c++ clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer
Projects
None yet
Development

No branches or pull requests

3 participants