Skip to content

Instantly share code, notes, and snippets.

@RReverser
Forked from shardest/.gitignore
Last active November 2, 2022 01:51
Show Gist options
  • Select an option

  • Save RReverser/633123e0c102a231e19080b25e563601 to your computer and use it in GitHub Desktop.

Select an option

Save RReverser/633123e0c102a231e19080b25e563601 to your computer and use it in GitHub Desktop.

Revisions

  1. RReverser revised this gist Nov 2, 2022. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion .gitignore
    Original file line number Diff line number Diff line change
    @@ -1 +0,0 @@
    warnglobals.so
  2. RReverser revised this gist Oct 31, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ClangWarnGlobals.cpp
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@ class PrintGlobalsVisitor : public clang::RecursiveASTVisitor<PrintGlobalsVisito
    bool VisitVarDecl(clang::VarDecl *D) {
    const clang::SourceManager &SM = Context->getSourceManager();
    if (D->hasGlobalStorage() && !D->getType().isConstQualified()) {
    clang::FullSourceLoc loc = Context->getFullLoc(D->getLocStart());
    clang::FullSourceLoc loc = Context->getFullLoc(D->getBeginLoc());
    if (!SM.isInSystemHeader(loc)) {
    clang::DiagnosticsEngine &D = *Diagnostics;
    unsigned int id = D.getCustomDiagID(clang::DiagnosticsEngine::Warning, "global variable");
  3. @shardest shardest revised this gist Apr 23, 2020. 2 changed files with 5 additions and 5 deletions.
    6 changes: 3 additions & 3 deletions ClangWarnGlobals.cpp
    Original file line number Diff line number Diff line change
    @@ -49,8 +49,8 @@ class PrintGlobalsConsumer : public clang::ASTConsumer {

    class PrintGlobalsAction : public clang::PluginASTAction {
    protected:
    clang::ASTConsumer *CreateASTConsumer(clang::CompilerInstance &CI, llvm::StringRef) {
    return new PrintGlobalsConsumer(&CI.getASTContext(), &CI.getDiagnostics());
    std::unique_ptr<clang::ASTConsumer> CreateASTConsumer(clang::CompilerInstance &CI, llvm::StringRef) {
    return std::unique_ptr<clang::ASTConsumer>(new PrintGlobalsConsumer(&CI.getASTContext(), &CI.getDiagnostics()));
    }

    bool ParseArgs(const clang::CompilerInstance &CI,
    @@ -63,4 +63,4 @@ class PrintGlobalsAction : public clang::PluginASTAction {
    }

    static clang::FrontendPluginRegistry::Add<PrintGlobalsAction>
    X("warn-globals", "generate warnings for non-const global variables");
    X("warn-globals", "generate warnings for non-const global variables");
    4 changes: 2 additions & 2 deletions Makefile
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    warnglobals.so: ClangWarnGlobals.cpp
    clang++ -g -O0 -std=c++98 -shared -fPIC $$(llvm-config --cflags --ldflags --libs support mc) -o "$@" $^ -lclang
    clang++ -g -O0 -shared -fPIC $$(llvm-config --cflags --cxxflags --ldflags --libs support mc) -o "$@" $^ -lclang

    .PHONY: run
    run: warnglobals.so
    clang++ -Xclang -load -Xclang ./warnglobals.so -Xclang -plugin -Xclang warn-globals test.cpp
    clang++ -Xclang -load -Xclang ./warnglobals.so -Xclang -add-plugin -Xclang warn-globals test.cpp

    .PHONY: clean
    clean:
  4. @sgielen sgielen revised this gist Jan 29, 2014. 1 changed file with 0 additions and 44 deletions.
    44 changes: 0 additions & 44 deletions ClangWarnGlobals.cpp
    Original file line number Diff line number Diff line change
    @@ -5,50 +5,6 @@
    //
    // Written by John Bartholomew <jpa.bartholomew@gmail.com>

    // Makefile
    #if 0
    warnglobals.so: WarnGlobals.cpp
    clang++ -std=c++98 -shared -fPIC $$(llvm-config --cflags --libs support mc) -o "$@" $^ -L/usr/lib/llvm/ -lclang

    .PHONY: run
    run: warnglobals.so
    clang++ -fsyntax-only -Xclang -load -Xclang ./warnglobals.so -Xclang -plugin -Xclang warn-globals test.cpp

    .PHONY: clean
    clean:
    rm -f warnglobals.so
    #endif

    // TEST FILE -- copy out to test.cpp
    #if 0
    #include <iostream>

    static int s_some_global = 42;
    static const int s_some_const_global = 42;

    static void some_function_with_a_static() {
    static int s_static_local = 42;
    }

    namespace some_namespace {
    namespace some_sub_namespace {
    int public_global = 42;
    }

    class some_class {
    static const int static_const_member = 42;
    static int static_member;
    };
    }

    int some_namespace::some_class::static_member = 42;

    int main(int argc, char **argv) {
    std::cout << "global value: " << s_some_global << "\n";
    return 0;
    }
    #endif

    #include "clang/Frontend/FrontendPluginRegistry.h"
    #include "clang/AST/AST.h"
    #include "clang/AST/ASTConsumer.h"
  5. @sgielen sgielen created this gist Jan 29, 2014.
    1 change: 1 addition & 0 deletions .gitignore
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    warnglobals.so
    110 changes: 110 additions & 0 deletions ClangWarnGlobals.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,110 @@
    // -Wglobals Clang plugin
    //
    // Based on example plugins and searching Clang's API documentation.
    // BEWARE! This is my first ever attempt at a Clang plugin.
    //
    // Written by John Bartholomew <jpa.bartholomew@gmail.com>

    // Makefile
    #if 0
    warnglobals.so: WarnGlobals.cpp
    clang++ -std=c++98 -shared -fPIC $$(llvm-config --cflags --libs support mc) -o "$@" $^ -L/usr/lib/llvm/ -lclang

    .PHONY: run
    run: warnglobals.so
    clang++ -fsyntax-only -Xclang -load -Xclang ./warnglobals.so -Xclang -plugin -Xclang warn-globals test.cpp

    .PHONY: clean
    clean:
    rm -f warnglobals.so
    #endif

    // TEST FILE -- copy out to test.cpp
    #if 0
    #include <iostream>

    static int s_some_global = 42;
    static const int s_some_const_global = 42;

    static void some_function_with_a_static() {
    static int s_static_local = 42;
    }

    namespace some_namespace {
    namespace some_sub_namespace {
    int public_global = 42;
    }

    class some_class {
    static const int static_const_member = 42;
    static int static_member;
    };
    }

    int some_namespace::some_class::static_member = 42;

    int main(int argc, char **argv) {
    std::cout << "global value: " << s_some_global << "\n";
    return 0;
    }
    #endif

    #include "clang/Frontend/FrontendPluginRegistry.h"
    #include "clang/AST/AST.h"
    #include "clang/AST/ASTConsumer.h"
    #include "clang/AST/RecursiveASTVisitor.h"
    #include "clang/Frontend/CompilerInstance.h"

    namespace {

    class PrintGlobalsVisitor : public clang::RecursiveASTVisitor<PrintGlobalsVisitor> {
    public:
    explicit PrintGlobalsVisitor(clang::ASTContext *Context, clang::DiagnosticsEngine *Diagnostics)
    : Context(Context), Diagnostics(Diagnostics) {}

    bool VisitVarDecl(clang::VarDecl *D) {
    const clang::SourceManager &SM = Context->getSourceManager();
    if (D->hasGlobalStorage() && !D->getType().isConstQualified()) {
    clang::FullSourceLoc loc = Context->getFullLoc(D->getLocStart());
    if (!SM.isInSystemHeader(loc)) {
    clang::DiagnosticsEngine &D = *Diagnostics;
    unsigned int id = D.getCustomDiagID(clang::DiagnosticsEngine::Warning, "global variable");
    D.Report(loc, id);
    }
    }
    return true;
    }
    private:
    clang::ASTContext *Context;
    clang::DiagnosticsEngine *Diagnostics;
    };

    class PrintGlobalsConsumer : public clang::ASTConsumer {
    public:
    explicit PrintGlobalsConsumer(clang::ASTContext *Context, clang::DiagnosticsEngine *Diagnostics)
    : Visitor(Context, Diagnostics) {}

    virtual void HandleTranslationUnit(clang::ASTContext &Context) {
    Visitor.TraverseDecl(Context.getTranslationUnitDecl());
    }
    private:
    PrintGlobalsVisitor Visitor;
    };

    class PrintGlobalsAction : public clang::PluginASTAction {
    protected:
    clang::ASTConsumer *CreateASTConsumer(clang::CompilerInstance &CI, llvm::StringRef) {
    return new PrintGlobalsConsumer(&CI.getASTContext(), &CI.getDiagnostics());
    }

    bool ParseArgs(const clang::CompilerInstance &CI,
    const std::vector<std::string>& args) {
    // To be written...
    return true;
    }
    };

    }

    static clang::FrontendPluginRegistry::Add<PrintGlobalsAction>
    X("warn-globals", "generate warnings for non-const global variables");
    10 changes: 10 additions & 0 deletions Makefile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    warnglobals.so: ClangWarnGlobals.cpp
    clang++ -g -O0 -std=c++98 -shared -fPIC $$(llvm-config --cflags --ldflags --libs support mc) -o "$@" $^ -lclang

    .PHONY: run
    run: warnglobals.so
    clang++ -Xclang -load -Xclang ./warnglobals.so -Xclang -plugin -Xclang warn-globals test.cpp

    .PHONY: clean
    clean:
    rm -f warnglobals.so
    26 changes: 26 additions & 0 deletions test.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    #include <iostream>

    static int s_some_global = 42;
    static const int s_some_const_global = 42;

    static void some_function_with_a_static() {
    static int s_static_local = 42;
    }

    namespace some_namespace {
    namespace some_sub_namespace {
    int public_global = 42;
    }

    class some_class {
    static const int static_const_member = 42;
    static int static_member;
    };
    }

    int some_namespace::some_class::static_member = 42;

    int main(int argc, char **argv) {
    std::cout << "global value: " << s_some_global << "\n";
    return 0;
    }