clang-tools 6.0.0

IncorrectRoundings.h
Go to the documentation of this file.
1 //===--- IncorrectRoundings.h - clang-tidy ----------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_INCORRECTROUNDINGS_H_
11 #define LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_INCORRECTROUNDINGS_H_
12
13 #include "../ClangTidy.h"
14
15 namespace clang {
16 namespace tidy {
17 namespace misc {
18
19 /// \brief Checks the usage of patterns known to produce incorrect rounding.
20 /// Programmers often use
21 /// (int)(double_expression + 0.5)
22 /// to round the double expression to an integer. The problem with this
23 /// 1. It is unnecessarily slow.
24 /// 2. It is incorrect. The number 0.499999975 (smallest representable float
25 /// number below 0.5) rounds to 1.0. Even worse behavior for negative
26 /// numbers where both -0.5f and -1.4f both round to 0.0.
27 class IncorrectRoundings : public ClangTidyCheck {
28 public:
29 IncorrectRoundings(StringRef Name, ClangTidyContext *Context)
30 : ClangTidyCheck(Name, Context) {}
31 void registerMatchers(ast_matchers::MatchFinder *Finder) override;
32 void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
33 };
34
35 } // namespace misc
36 } // namespace tidy
37 } // namespace clang
38
39 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_TIDY_MISC_INCORRECTROUNDINGS_H_
Name
StringHandle Name
Definition: PreprocessorTracker.cpp:525
clang::tidy::misc::IncorrectRoundings::IncorrectRoundings
IncorrectRoundings(StringRef Name, ClangTidyContext *Context)
Definition: IncorrectRoundings.h:29
clang::tidy::ClangTidyCheck
Base class for all clang-tidy checks.
Definition: ClangTidy.h:127
clang::tidy::misc::IncorrectRoundings
Checks the usage of patterns known to produce incorrect rounding.
Definition: IncorrectRoundings.h:27
clang::tidy::misc::IncorrectRoundings::check
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
Definition: IncorrectRoundings.cpp:62
clang
Definition: AndroidTidyModule.cpp:28
clang::tidy::ClangTidyContext
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
Definition: ClangTidyDiagnosticConsumer.h:104
clang::tidy::misc::IncorrectRoundings::registerMatchers
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
Definition: IncorrectRoundings.cpp:34