source: src/Validate/CompoundLiteral.cpp @ 658f3179

Last change on this file since 658f3179 was 35cc6d4, checked in by Michael Brooks <mlbrooks@…>, 6 weeks ago

Mitigate several unused-declaration warnings in generated code.

See tests/nowarn/unused for the specific cases.

  • Property mode set to 100644
File size: 1.9 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2018 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// CompoundLiteral.cpp -- Use variables to implement compound literals.
8//
9// Author           : Andrew Beach
10// Created On       : Mon Nov 15 16:33:00 2021
11// Last Modified By : Andrew Beach
12// Last Modified On : Mon Nov 16  9:47:00 2021
13// Update Count     : 0
14//
15
16#include "CompoundLiteral.hpp"
17
18#include "AST/Decl.hpp"
19#include "AST/Expr.hpp"
20#include "AST/Pass.hpp"
21#include "AST/TranslationUnit.hpp"
22#include "Common/UniqueName.hpp"
23
24namespace Validate {
25
26namespace {
27
28struct CompoundLiteral final :
29                public ast::WithDeclsToAdd {
30        ast::Storage::Classes storageClasses;
31
32        void previsit( const ast::ObjectDecl * decl );
33        const ast::Expr * postvisit( const ast::CompoundLiteralExpr * expr );
34};
35
36void CompoundLiteral::previsit( const ast::ObjectDecl * decl ) {
37        storageClasses = decl->storage;
38}
39
40const ast::Expr * CompoundLiteral::postvisit(
41                const ast::CompoundLiteralExpr * expr ) {
42        static UniqueName litName( "_compLit" );
43
44        // Transform: [storageClasses] ... (struct S){...} ...
45        // Into:      [storageClasses] struct S _compLit = {...};
46        //                             ... _compLit ...
47        ast::ObjectDecl * temp = new ast::ObjectDecl(
48                expr->location,
49                litName.newName(),
50                expr->result,
51                expr->init,
52                storageClasses
53                );
54        // FIXME: A resolution of #280 could make the unused attribute unnecessary here
55        //     (let test nowarn/unused decide)
56        temp->attributes.push_back( new ast::Attribute( "unused" ) );
57        declsToAddBefore.push_back( temp );
58        return new ast::VariableExpr( expr->location, temp );
59}
60
61} // namespace
62
63void handleCompoundLiterals( ast::TranslationUnit & translationUnit ) {
64        ast::Pass<CompoundLiteral>::run( translationUnit );
65}
66
67} // namespace Validate
68
69// Local Variables: //
70// tab-width: 4 //
71// mode: c++ //
72// compile-command: "make install" //
73// End: //
Note: See TracBrowser for help on using the repository browser.